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: Problem with doctest and decorated functions
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 2.7
process
Status: closed Resolution: accepted
Dependencies: Superseder:
Assigned To: skip.montanaro Nosy List: danilo, skip.montanaro, steven.daprano, tim.peters
Priority: normal Keywords:

Created on 2007-09-05 10:53 by danilo, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
doctest.py.patch danilo, 2007-09-06 21:43
doctestfail.py steven.daprano, 2009-02-14 02:06 Script demonstrating the failure of doctest on decorated functions.
Messages (5)
msg55660 - (view) Author: Daniel Larsson (danilo) Date: 2007-09-05 10:53
Seems like doctest won't recognize functions inside the module under
test are actually in that module, if the function is decorated by a
decorator that wraps the function in an externally defined function,
such as in this silly example:

# decorator.py
import functools

def simplelog(f):
    @functools.wraps(f)
    def new_f(*args, **kwds):
        print "Wrapper calling func"
        return f(*args, **kwds)
    return new_f

# test.py
from decorator import simplelog

@simplelog
def test():
    """
    This test should fail, since the decorator prints output.
    Seems I don't get called though
    >>> test()
    'works!'
    """
    return "works!"

if __name__ == '__main__':
    import doctest
    doctest.testmod()

--

The problem lies in DocTestFinder._from_module, which checks if the
function's func_globals attribute is the same as the module's __dict__
attribute.

I'd propose to do the __module__/inspect.getmodule() checks (aren't they
 both checking the same thing btw?) before the inspect.isfunction check.
msg55712 - (view) Author: Daniel Larsson (danilo) Date: 2007-09-06 21:43
Here's a patch that alters the order of checks in DocTestFinder._from_module
msg76037 - (view) Author: Skip Montanaro (skip.montanaro) * (Python triager) Date: 2008-11-19 03:36
I applied this patch to my trunk sandbox.  It seems to solve the problem
I just encountered where doctests are hidden in decorated functions &
tests pass.  Checked in as r67277.  Should be backported to 2.6 and
forward ported to 3.0.
msg81985 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2009-02-14 02:06
For what it's worth, this bug appears to go back to at least Python 2.4,
and it affects functions using decorators even if they are defined in
the same module as the decorated function. I've applied the patch to my
2.4 installation, and it doesn't fix the issue. I'd like to request this
be reopened, because I don't believe the patch works as advertised.

I've attached a simple script which should demonstrate the issue. Run it
with "python doctestfail.py [-v]", and if it passes with no failures,
the bug still exists. I've tested it on 2.4 before and after the patch.
(Apologies for not having anything more recent at the moment.)
msg82139 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2009-02-15 04:47
Earlier I wrote:

"I've applied the patch to my 2.4 installation, and it doesn't fix the
issue. I'd like to request this be reopened, because I don't believe the
patch works as advertised."

Nevermind, I withdraw the request. I believe I was mislead due to the
decorated function not having it's docstring automatically copied from
the original without the use of functools.wraps().
History
Date User Action Args
2022-04-11 14:56:26adminsetgithub: 45449
2018-12-12 06:46:50serhiy.storchakalinkissue6835 superseder
2009-02-15 04:47:34steven.dapranosetmessages: + msg82139
2009-02-14 02:06:19steven.dapranosetfiles: + doctestfail.py
nosy: + steven.daprano
messages: + msg81985
2008-11-19 03:38:45skip.montanarosetstage: patch review -> resolved
2008-11-19 03:36:39skip.montanarosetstatus: open -> closed
versions: + Python 2.7, - Python 2.5
nosy: + skip.montanaro
messages: + msg76037
assignee: tim.peters -> skip.montanaro
resolution: accepted
stage: patch review
2007-09-17 08:35:16jafosetpriority: normal
assignee: tim.peters
nosy: + tim.peters
2007-09-06 21:43:25danilosetfiles: + doctest.py.patch
messages: + msg55712
2007-09-05 10:53:24danilocreate