Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #91506
| X-Received | by 10.68.180.5 with SMTP id dk5mr12237052pbc.3.1432937063548; Fri, 29 May 2015 15:04:23 -0700 (PDT) |
|---|---|
| X-Received | by 10.182.50.136 with SMTP id c8mr84519obo.10.1432937063480; Fri, 29 May 2015 15:04:23 -0700 (PDT) |
| Path | csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!h15no423184igd.0!news-out.google.com!kd3ni31446igb.0!nntp.google.com!h15no234925igd.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail |
| Newsgroups | comp.lang.python |
| Date | Fri, 29 May 2015 15:04:23 -0700 (PDT) |
| Complaints-To | groups-abuse@google.com |
| Injection-Info | glegroupsg2000goo.googlegroups.com; posting-host=64.22.160.1; posting-account=UdR4xAoAAAArgvobUNiC536uU9AtFlFD |
| NNTP-Posting-Host | 64.22.160.1 |
| User-Agent | G2/1.0 |
| MIME-Version | 1.0 |
| Message-ID | <c441b480-a5e1-45ff-b4d2-e50bb9d5c5f0@googlegroups.com> (permalink) |
| Subject | functools.wraps does not play nice with doc tests? |
| From | Vlad <VladTheCompiler@gmail.com> |
| Injection-Date | Fri, 29 May 2015 22:04:23 +0000 |
| Content-Type | text/plain; charset=ISO-8859-1 |
| Content-Transfer-Encoding | quoted-printable |
| Xref | csiph.com comp.lang.python:91506 |
Show key headers only | View raw
Hello,
So, I know this topic comes up a lot, but I haven't been able to find any discussion on this particular twist on the topic. Perhaps someone has some insight.
So, I have a function, which is decorated. In order for the doctest test finder to find the doc tests in the decorated function, my wrapper needs to use the functools.wraps decorator inside it. So far so good. *However*, the issue is that if the decorator and the decorated function are defined in different modules, and the decorator is defined at a greater line number in its module than the decorated function in its module, and the doc test fails, the test report lists the failure as occurring at an "unknown line number". This is a problem if you have additional logic built on top of the reporting that relies on line numbers of test failures (and even more fundamentally, it's a pain to manually find which test actually failed).
Here is a specific example, this is running Python 2.7.9, in case that matters.
File decorator.py
1 import functools
2
3
4
5
6
7
8
9 # The decorator has to be defined at a line number greater than the
10 # decorated function in order to trigger the bug.
11 def decorator(func):
12 @functools.wraps(func)
13 def wrapper(*args, **kwargs):
14 print('In Wrapper!')
15 return func(*args, **kwargs)
16 return wrapper
File decorated.py
1 from . import decorator
2
3
4 @decorator.decorator
5 def my_func():
6 '''
7 >>> my_func()
8 '''
9 print('my_func!')
10
11
12 if __name__ == '__main__':
13 import doctest
14 doctest.testmod()
Now, let's try to run the tests:
> python -m decorated
**********************************************************************
File ".../decorated.py", line ?, in __main__.my_func
Failed example:
my_func()
Expected nothing
Got:
In Wrapper!
my_func!
**********************************************************************
1 items had failures:
1 of 1 in __main__.my_func
***Test Failed*** 1 failures.
So, the test ran, and failed as we wanted, but the line number was not detected correctly. Has anyone run into this issue, and/or know a workaround? Or perhaps I am missing something obvious here?
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
functools.wraps does not play nice with doc tests? Vlad <VladTheCompiler@gmail.com> - 2015-05-29 15:04 -0700
csiph-web