Issue 1108: Problem with doctest and decorated functions (original) (raw)

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:26 admin set github: 45449
2018-12-12 06:46:50 serhiy.storchaka link issue6835 superseder
2009-02-15 04:47:34 steven.daprano set messages: +
2009-02-14 02:06:19 steven.daprano set files: + doctestfail.pynosy: + steven.dapranomessages: +
2008-11-19 03:38:45 skip.montanaro set stage: patch review -> resolved
2008-11-19 03:36:39 skip.montanaro set status: open -> closedversions: + Python 2.7, - Python 2.5nosy: + skip.montanaromessages: + assignee: tim.peters -> skip.montanaroresolution: acceptedstage: patch review
2007-09-17 08:35:16 jafo set priority: normalassignee: tim.petersnosy: + tim.peters
2007-09-06 21:43:25 danilo set files: + doctest.py.patchmessages: +
2007-09-05 10:53:24 danilo create