doctest and decorators

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Wed Sep 5 01:46:19 CEST 2007


En Tue, 04 Sep 2007 19:29:11 -0300, Daniel Larsson  
<daniel.j.larsson at gmail.com> escribi�:

> On 9/5/07, Ferenczi Viktor <python at cx.hu> wrote:
>>
>> > > @functools.wraps(f)
>> > > Pass the function to be wrapped by the decorator to the wraps
>> function.
>> > Ooops, right. That doesn't change the fact that decorated functions  
>> get
>> > hidden from doctest though.
>
>
> I have no issue when the decorator is defined in the same module as the
> decorated function, my problem is running doctests on functions using an
> imported decorator. Having to implement the decorator in every source  
> module
> isn't very practical. Try splitting your module in two, as I did, and run
> with -v, and you'll see the problem.

Looks like a bug. doctest is failing to recognize that the decorated  
function belongs to the module being tested.

A simple patch: In doctest.py, method _from_module, near line 840, you  
have these lines:

         elif inspect.getmodule(object) is not None:
             return module is inspect.getmodule(object)

Move them up, just before the line:

         elif inspect.isfunction(object):

This works fine in this case, but I'm not sure whether this breaks in  
other circumstances (but I can't think of a case when using  
inspect.getmodule() would not be appropiate).

PS: I can't see any tests for decorated functions (except  
@classmethod/@staticmethod) in the library test suite. I'll try to add  
some and submit a patch.

-- 
Gabriel Genellina




More information about the Python-list mailing list