This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: doctest.py should include method descriptors when looking inside a class __dict__
Type: enhancement Stage: patch review
Components: Library (Lib) Versions: Python 3.2
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: brian.curtin, daaku, filip.zyzniewski, georg.brandl, ncoghlan, r.david.murray, steven.daprano, terry.reedy
Priority: normal Keywords: needs review, patch

Created on 2008-10-04 00:13 by daaku, last changed 2022-04-11 14:56 by admin.

Files
File name Uploaded Description Edit
patch steven.daprano, 2010-02-07 00:44 patch to add support for method descriptors to doctest.DocTestFinder
doctest_test.py steven.daprano, 2010-02-09 13:26
doctest_patch steven.daprano, 2010-03-20 13:16 patch to add support for method descriptors to doctest.DocTestFinder, including updated test for test.test_doctest2
Messages (9)
msg74285 - (view) Author: (daaku) Date: 2008-10-04 00:13
doctest.py currently does not include doctests from method descriptors 
in a class.

The patch is simple, in the _find function in class DocTestFinder:

Original:
                # Recurse to methods, properties, and nested classes.
                if ((inspect.isfunction(val) or inspect.isclass(val) or
                      isinstance(val, property)) and
                      self._from_module(module, val)):

Patched:
                # Recurse to methods, properties, and nested classes.
                if ((inspect.isfunction(val) or inspect.isclass(val) or
                      inspect.ismethoddescriptor(val) or
                      isinstance(val, property)) and
                      self._from_module(module, val)):

Adding the "inspect.ismethoddescriptor(val) or" line.
msg98974 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2010-02-07 00:44
The patch you suggest is *not* sufficient, at least not by my testing.

However, the attached patch does work, according to my tests.
msg98983 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2010-02-07 05:53
I am not sure whether this would be considered a bugfix or a new feature.
(ie, whether it would apply to 2.6 and 3.1 or not)

But the change is needed for 3.x also.

I am not sure whether the doc needs changing. A testcase should be added to doctest-test.
msg99115 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2010-02-09 13:26
Attached is a simple test script for the patch I submitted. I have tested it with Python 2.6 both before and after applying the patch. Run it from the command line.

With the unpatched doctest module, it prints:

"Expected 2 doctests, but only found 1"

After the patch, it finds and runs both doctests, and prints nothing.
msg99145 - (view) Author: Brian Curtin (brian.curtin) * (Python committer) Date: 2010-02-10 01:31
Can you add your test(s) in Lib/test/test_doctest.py ? That way it will be run with the Python regression suite. Ideally a documentation update would come with the patch. Also, line length should ideally be capped at 79 characters (re: PEP-8).

Might as well move the valname string building outside of both if-tests since it's common to both of them.


Terry: this looks like a feature rather than a bug, removing 2.6/3.1.
msg101372 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2010-03-20 13:16
I have fixed the issue with line length, and taken Brian's advice re valname. Updated patch for doctest and test.test_doctest2 is attached.
msg104300 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2010-04-27 07:13
So for staticmethods and classmethods, valname doesn't need to be reassigned?
msg110690 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2010-07-18 21:36
Could someone please respond to Georg's comment msg104300, thanks.
msg205905 - (view) Author: Filip Zyzniewski (filip.zyzniewski) Date: 2013-12-11 13:25
It seems like there is also an issue with property classes defined in a different module.

In the example below Foo.x uses standard property, and Foo.y uses prop imported from the prop module.

This results in docstring for Foo.y to be missed:

filip@klocek:~/test$ cat prop.py
class prop(property):
    pass
filip@klocek:~/test$ cat foo.py
from prop import prop

class Foo(object):

    @property
    def x(self):
        """
        >>> Foo().x
        'x'
        """
        return 'x'

    @prop
    def y(self):
        """
        >>> Foo().y
        'y'
        """
        return 'y'
filip@klocek:~/test$ python --version
Python 2.7.3
filip@klocek:~/test$ python -m doctest foo.py -v
Trying:
    Foo().x
Expecting:
    'x'
ok
2 items had no tests:
    foo
    foo.Foo
1 items passed all tests:
   1 tests in foo.Foo.x
1 tests in 3 items.
1 passed and 0 failed.
Test passed.
filip@klocek:~/test$ python3 --version
Python 3.2.3
filip@klocek:~/test$ python3 -m doctest foo.py -v
Trying:
    Foo().x
Expecting:
    'x'
ok
2 items had no tests:
    foo
    foo.Foo
1 items passed all tests:
   1 tests in foo.Foo.x
1 tests in 3 items.
1 passed and 0 failed.
Test passed.
filip@klocek:~/test$
History
Date User Action Args
2022-04-11 14:56:40adminsetgithub: 48287
2014-02-03 15:42:56BreamoreBoysetnosy: - BreamoreBoy
2013-12-11 14:02:17r.david.murraysetnosy: + r.david.murray
2013-12-11 13:25:49filip.zyzniewskisetnosy: + filip.zyzniewski
messages: + msg205905
2010-11-12 05:33:12terry.reedysetversions: - Python 2.7
2010-07-18 21:36:25BreamoreBoysetnosy: + BreamoreBoy
messages: + msg110690
2010-04-27 07:13:35georg.brandlsetnosy: + georg.brandl
messages: + msg104300
2010-03-20 13:16:47steven.dapranosetfiles: + doctest_patch

messages: + msg101372
2010-02-11 12:40:53ncoghlansetnosy: + ncoghlan
2010-02-10 01:31:47brian.curtinsetpriority: normal

type: enhancement
versions: - Python 2.6, Python 3.1
keywords: + patch, needs review
nosy: + brian.curtin

messages: + msg99145
stage: patch review
2010-02-09 13:26:49steven.dapranosetfiles: + doctest_test.py

messages: + msg99115
2010-02-07 05:53:03terry.reedysetnosy: + terry.reedy

messages: + msg98983
versions: + Python 3.1, Python 2.7, Python 3.2
2010-02-07 00:44:48steven.dapranosetfiles: + patch
nosy: + steven.daprano
messages: + msg98974

2008-10-04 00:13:25daakucreate