473,402 Members | 2,046 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

Decorator cllass hides docstring from doctest?

Saving the following code to a file and running the code through
python does not give the expected error. disableling the "@decor" line
leads to the expected error message. Is this a bug or an overseen
feature?
--- snip dectest.py ---
class decor(object):
def __init__(self, f):
self.f = f
def __call__(self, *args, **kw):
return self.f(*args, **kw)

@decor
def f(a, b):
"""
>>f(1,2)
False
>>f(2,2)
False
"""
return a == b

def _test():
import doctest
doctest.testmod()

if __name__ == "__main__":
_test()
--- snip dectest.py ---

Our Python is:

Python 2.4.2 (#1, Dec 5 2005, 10:13:23)
[GCC 3.3.5 20050117 (prerelease) (SUSE Linux)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Thanks
Berthold
--
__ Address:
G / \ L Germanischer Lloyd
phone: +49-40-36149-7374 -+----+- Vorsetzen 35 P.O.Box 111606
fax : +49-40-36149-7320 \__/ D-20459 Hamburg D-20416 Hamburg

Sep 21 '06 #1
3 1920
be******@despammed.com (Berthold =?iso-8859-15?Q?H=F6llmann?=) wrote:
Saving the following code to a file and running the code through
python does not give the expected error. disableling the "@decor" line
leads to the expected error message. Is this a bug or an overseen
feature?
It's a problem with your implementation of the decorator. In fact it is two
problems: the decorated 'f' is a class instance so doctest ignores it, and
it doesn't have a docstring so doctest ignores it.

If you rewrite the decorator so that the decorated 'f' is still a function
and preserve the docstring then it works as you might expect. e.g.

def decor(f):
def wrapper(*args, **kw):
return f(*args, **kw)
wrapper.__doc__ = f.__doc__
wrapper.__name__ = f.__name__
return wrapper
Sep 21 '06 #2
Berthold Höllmann wrote:
Saving the following code to a file and running the code through
python does not give the expected error. disableling the "@decor" line
leads to the expected error message. Is this a bug or an overseen
feature?
Others have already pointed out the mistake. I wrote a module to avoid
this kind of issues, so you
may want to check it out:

http://www.phyast.pitt.edu/~micheles...mentation.html

Michele Simionato

Sep 21 '06 #3
Berthold Höllmann wrote:
Saving the following code to a file and running the code through
python does not give the expected error. disableling the "@decor" line
leads to the expected error message. Is this a bug or an overseen
feature?
Neither, I'd say. Just an unfortunate interaction between doctest and
function decoration. For the most common case where one function is wrapped
by another, Python 2.5 has grown a "meta-decorator"
>>import functools
def deco(f):
.... @functools.wraps(f)
.... def g():
.... f()
.... return g
....
>>@deco
.... def f():
.... "yadda yadda"
....
>>f.__doc__
'yadda yadda'

but with callable objects you're on your own, I think.
I came up with:

def register_doctest(name, doc):
global __test__
if doc:
try:
__test__
except NameError:
__test__ = {name: doc}
else:
if name in __test__:
raise ValueError("name clash")
__test__[name] = doc

class decor(object):
def __init__(self, f):
self.f = f
register_doctest(f.__name__, f.__doc__)
def __call__(self, *args, **kw):
return self.f(*args, **kw)

@decor
def f(a, b):
"""
>>f(1,2)
False
>>f(2,2)
False
"""
return a == b

import doctest
doctest.testmod()

Peter
Sep 21 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: barnesc | last post by:
I'm revising my vote. Thanks for the responses. I vote against: class foo: @synchronized @types("o,i,i") @author('Chris King')
37
by: Bengt Richter | last post by:
ISTM that @limited_expression_producing_function @another def func(): pass is syntactic sugar for creating a hidden list of functions. (Using '|' in place of '@' doesn't change the picture...
7
by: Peter Otten | last post by:
I'm sure they have been mentioned somewhere but here are some more advantages of a decorator keyword (I use "transform"): - The docstring can be moved to the top of the decorator suite. - Simple...
99
by: Paul McGuire | last post by:
There are a number of messages on the python-dev mail list that indicate that Guido is looking for some concensus to come from this list as to what *one* alternative syntax for decorators we would...
17
by: Jim Jewett | last post by:
Guido has said that he is open to considering *one* alternative decorator syntax. At the moment, (Phillip Eby's suggestion) J4 <URL: http://www.python.org/moin/PythonDecorators > (section 5.21...
0
by: Carlos Ribeiro | last post by:
Hello all, While studying some of the templating systems available for Python, I've come up with an idea to implement templates using decorators. The following code snippet runs in Python 2.3; a...
2
by: WaterWalk | last post by:
Hello, I was recently learning python decorator and descriptor and emulated a @classmethod decorator: class EmuClassMethod(object): def __init__(self, f=None): self.f = f def __get__(self, obj,...
3
by: =?iso-8859-1?q?KLEIN_St=E9phane?= | last post by:
Hi, for example, in http://svn.zope.org/zc.buildout/trunk/src/zc/buildout/ tests.py?rev=89831&view=auto test file, there is this doctests : def develop_verbose(): """ We should be able to...
2
by: mde | last post by:
I'm wondering if there is a "best practice" for *creating doctests in methods* that reduces clutter/duplication of dummy instantiations. In the following code there are five (labeled 1-5) possible...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.