[Python-Dev] @deprecated (was: Useful thread project for 2.5?)
Jim Jewett
jimjjewett at gmail.com
Wed Mar 9 00:05:05 CET 2005
Greg wished for:
... 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.
I agree that it should go in the cookbook, but I think you should
set the category to a DeprecationWarning and give the offending
function's name. (Whether to worry about suppression of calls
to *other* deprecated functions -- I think is out of scope for a recipe.)
def newFunc(*args, **kwargs):
warnings.warn("Call to deprecated function %s" % func.__name__,
category=DeprecationWarning)
return func(*args, **kwargs)
-jJ
More information about the Python-Dev
mailing list