doctests and decorators

Eric Snow esnow at verio.net
Tue Jun 16 13:36:00 EDT 2009


On Jun 16, 11:24 am, Michele Simionato <michele.simion... at gmail.com>
wrote:
> On Jun 16, 6:39 pm, Eric Snow <es... at verio.net> wrote:
>
>
>
> > On Jun 16, 10:31 am, Christian Heimes <li... at cheimes.de> wrote:
>
> > > Eric Snow schrieb:
>
> > > > 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):
>
> > > >http://bugs.python.org/issue1108
> > > >http://mail.python.org/pipermail/python-list/2007-September/627866.html
>
> > > > 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
> > > > used:
>
> > > It's not an issue with the doctest module but with your code. You want
> > > to use functools.wraps().
>
> > >http://docs.python.org/library/functools.html#functools.wraps
>
> > > Christian
>
> > Unfortunately, I am stuck on 2.4 for now, which does not have the
> > functools.
>
> > -eric
>
> But you can always use the decorator module:http://pypi.python.org/pypi/decorator

Thanks to both of you.  Very helpful.

So in general should decorators always hide themselves?  I am guessing
not, otherwise this would already be part of their behavior.  Still,
is it the common case to camouflage the decorator like this?  If so, I
would expect it to be the default behavior of decorators.

-eric



More information about the Python-list mailing list