Title: Problem with doctest and decorated functions
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 2.7
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 2009-02-15 04:47 by steven.daprano. This issue is now closed.

File name Uploaded Description Edit danilo, 2007-09-06 21:43 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:

import functools

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

from decorator import simplelog

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

if __name__ == '__main__':
    import doctest


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

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 [-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().
Date User Action Args
2018-12-12 06:46:50serhiy.storchakalinkissue6835 superseder
2009-02-15 04:47:34steven.dapranosetmessages: + msg82139
2009-02-14 02:06:19steven.dapranosetfiles: +
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: +
messages: + msg55712
2007-09-05 10:53:24danilocreate