Re: [Python-Dev] @deprecated (was: Useful thread project for 2.5?)
On Tuesday 08 March 2005 18:05, Jim Jewett wrote: ... compiler recognition of @deprecated in doc comments. Michael Chermside suggested: = code = import warnings def deprecated(func): This is a decorator which can be used to mark functions as deprecated. It will result in a warning being emmitted when the function is used. def newFunc(*args, **kwargs): warnings.warn(Call to deprecated function.) return func(*args, **kwargs) return newFunc = example = ... UserWarning: Call to deprecated function. This is a recipe for disaster. Creating a new function from the old can have unwanted side effects, since you effectively change the object. For example, if someone is monkey patching this function, then the deprecation warning gets lost. In Zope 3's deprecation package, we have decided to put a special deprecation proxy around the module (instead of the function) and register a list of attribute names (note that it does not matter whether its a class, function or other type of object) that are deprecated. The advantage is that you get a deprecation warning just by looking up the object. See: http://svn.zope.org/Zope3/trunk/src/zope/deprecation/ Disclaimer: This code is a bit experimental and might not be the best solution. It is currently used in the trunk and does a pretty good job, though. Regards, Stephan -- Stephan Richter CBU Physics Chemistry (B.S.) / Tufts Physics (Ph.D. student) Web2k - Web Software Design, Development and Training ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] @deprecated (was: Useful thread project for 2.5?)
This is a recipe for disaster. Creating a new function from the old can have unwanted side effects, since you effectively change the object. For example, if someone is monkey patching this function, then the deprecation warning gets lost. That's a rather extreme use case, and not one that IMO should block the @deprecated decorator from being used. I hope that monkey patching is rare enough that you shouldn't mind checking once a year of so if the thing you're monkey-patching might have been deprecated (in which case you shouldn't be monkey-patching it but instead rewrite your code to avoid it altogether). In Zope 3's deprecation package, we have decided to put a special deprecation proxy around the module (instead of the function) and register a list of attribute names (note that it does not matter whether its a class, function or other type of object) that are deprecated. The advantage is that you get a deprecation warning just by looking up the object. Yeah, but not everybody has Zope's proxying machinery. I think you're overreacting. -- --Guido van Rossum (home page: http://www.python.org/~guido/) ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
RE: [Python-Dev] @deprecated (was: Useful thread project for 2.5?)
Michael Chermside suggested: import warnings def deprecated(func): This is a decorator which can be used to mark functions as deprecated. It will result in a warning being emmitted when the function is used. def newFunc(*args, **kwargs): warnings.warn(Call to deprecated function.) return func(*args, **kwargs) return newFunc Decorators like this should preserve information about the underlying function: def deprecated(func): This is a decorator which can be used to mark functions as deprecated. It will result in a warning being emmitted when the function is used. def newFunc(*args, **kwargs): warnings.warn(Call to deprecated function.) return func(*args, **kwargs) newFunc.__name__ = func.__name__ newFunc.__doc__ = func.__doc__ newFunc.__dict__.update(func.__dict__) return newFunc Raymond ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com