doctest and decorators

Daniel Larsson daniel.j.larsson at gmail.com
Wed Sep 5 00:29:11 CEST 2007


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.

Run my test script (one file) with the -v (verbose) option. Without the -v
> option it does not show output. This fact is documented in the Python
> manual
> at the doctest module.
>
> --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
> import functools
>
> def simplelog(f):
>     @functools.wraps(f)
>     def new_f(*args, **kwds):
>         print "Wrapper calling func"
>         return f(*args, **kwds)
>     return new_f
>
> @simplelog
> def test():
>     """
>     >>> test()
>     Wrapper calling func
>     'works!'
>     """
>     return 'works!'
>
> def fn():
>     """
>     >>> fn()
>     'ok'
>     """
>     return 'ok'
>
> if __name__ == '__main__':
>     import doctest
>     doctest.testmod()
> --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
>
> Regard, Viktor
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20070905/99adc310/attachment.html>


More information about the Python-list mailing list