Re: doctest and decorators

Discussion in 'Python' started by Gabriel Genellina, Sep 5, 2007.

  1. En Tue, 04 Sep 2007 19:29:11 -0300, Daniel Larsson
    <> escribi�:

    > On 9/5/07, Ferenczi Viktor <> 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
    Gabriel Genellina, Sep 5, 2007
    #1
    1. Advertising

  2. > En Tue, 04 Sep 2007 19:29:11 -0300, Daniel Larsson
    > <> escribi?:
    >
    >
    >
    > > On 9/5/07, Ferenczi Viktor <> 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.


    I cannot reproduce your problem. Using functools.wraps
    the __module__ attribute is set correctly and everything
    works, even for decorators defined in separated modules.
    Care to post a complete example of what you are doing?

    Michele Simionato

    P.S. for some reason your messages are not appearing on
    Google groups, I see only the replies.
    Michele Simionato, Sep 5, 2007
    #2
    1. Advertising

  3. On 9/5/07, Daniel Larsson <> wrote:
    > Here's my two files again:
    >
    > # 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()
    >


    Ok, I could not see the code before (Google groups as getting worse as
    the time goes by). It looks like you are right, it is a bug in
    doctest.

    Michele Simionato
    Michele Simionato, Sep 5, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul M

    ps2, IDLE, and doctest

    Paul M, Oct 17, 2003, in forum: Python
    Replies:
    0
    Views:
    395
    Paul M
    Oct 17, 2003
  2. Arien Malec

    PEP 318 decorators are not Decorators

    Arien Malec, Aug 13, 2004, in forum: Python
    Replies:
    11
    Views:
    557
    Arien Malec
    Aug 16, 2004
  3. Michele Simionato

    issues with doctest and threads

    Michele Simionato, Aug 8, 2005, in forum: Python
    Replies:
    5
    Views:
    324
    Michele Simionato
    Aug 10, 2005
  4. John J Lee
    Replies:
    3
    Views:
    456
    bruno at modulix
    Dec 1, 2005
  5. Mattsteel
    Replies:
    4
    Views:
    348
    Mattsteel
    Jan 30, 2010
Loading...

Share This Page