doctests and decorators

Eric Snow esnow at
Tue Jun 16 11:59:40 EDT 2009

Apparently there is a known issue with doctests, in which tests in
functions using externally defined decorators are ignored.  The
recommended fix is to update the order of checks in the _from_module
method of DocTestFinder in the doctest module.  The bug and fix are
discussed at the following URLs (and several places in this group):

The fix implies that the inpect.getmodule function will find the
module of the function object and not of the decorator.  However, in
2.4 the inspect.getmodule function returns the module of the
decorator.  I have subsequently tested this in 2.5 and 2.6, and it
also returns the module of the decorator.  As such, the fix for
doctests does not work in my tests.  Below is the test code that I

def decorator(function):
    def new_function(*args, **kwargs):
        return function(*args, **kwargs)
    return new_function
import test1
import inspect

class Test(object):
    def test2(self): pass

def run_tests():
    test = Test()

    print("Test is class, test is instance, test2 is method of Test
(has decorator)")
    print("test's module:          %s" % inspect.getmodule(test))
    print("Test's module:          %s" % inspect.getmodule(Test))
    print("test.test2's module:    %s" % inspect.getmodule
    print("Test.test2's module:    %s" % inspect.getmodule
    print("test.test2's func_name: %s" % test.test2.func_name)
    print("Test.test2's func_name: %s" % Test.test2.func_name)

if __name__ == "__main__":


Here is the output that I got in 2.4, 2.5, and 2.6:

Test is class, test is instance, test2 is method of Test (has
test's module:          <module '__main__' from ''>
Test's module:          <module '__main__' from ''>
test.test2's module:    <module 'test1' from '/tmp/'>
Test.test2's module:    <module 'test1' from '/tmp/'>
test.test2's func_name: new_function
Test.test2's func_name: new_function

If things were working right, then the module for test.test2 would be
the same as the module for test.  I must be missing something, as the
referenced discussion suggests a simple conclusion.  Any ideas?


More information about the Python-list mailing list