50
65
# add that on top of the primitives, once we have all three written
69
def deprecation_string(a_callable, deprecation_version):
70
"""Generate an automatic deprecation string for a_callable.
72
:param a_callable: The callable to substitute into deprecation_version.
73
:param deprecation_version: A deprecation format warning string. This should
74
have a single %s operator in it. a_callable will be turned into a nice
75
python symbol and then substituted into deprecation_version.
77
if getattr(a_callable, 'im_class', None) is None:
78
symbol = "%s.%s" % (a_callable.__module__,
81
symbol = "%s.%s.%s" % (a_callable.im_class.__module__,
82
a_callable.im_class.__name__,
85
return deprecation_version % symbol
53
88
def deprecated_function(deprecation_version):
54
89
"""Decorate a function so that use of it will trigger a warning."""
59
94
def decorated_function(*args, **kwargs):
60
95
"""This is the decorated function."""
61
symbol = "%s.%s" % (callable.__module__,
64
warn(deprecation_version % symbol, DeprecationWarning, stacklevel=2)
96
warn(deprecation_string(callable, deprecation_version),
97
DeprecationWarning, stacklevel=2)
65
98
return callable(*args, **kwargs)
66
99
_populate_decorated(callable, deprecation_version, "function",
67
100
decorated_function)
140
176
decorated_callable.__module__ = callable.__module__
141
177
decorated_callable.__name__ = callable.__name__
142
178
decorated_callable.is_deprecated = True
181
def _dict_deprecation_wrapper(wrapped_method):
182
"""Returns a closure that emits a warning and calls the superclass"""
183
def cb(dep_dict, *args, **kwargs):
184
msg = 'access to %s' % (dep_dict._variable_name, )
185
msg = dep_dict._deprecation_version % (msg,)
187
msg += ' ' + dep_dict._advice
188
warn(msg, DeprecationWarning, stacklevel=2)
189
return wrapped_method(dep_dict, *args, **kwargs)
193
class DeprecatedDict(dict):
194
"""A dictionary that complains when read or written."""
204
"""Create a dict that warns when read or modified.
206
:param deprecation_version: something like zero_nine
207
:param initial_value: The contents of the dict
208
:param variable_name: This allows better warnings to be printed
209
:param advice: String of advice on what callers should do instead
210
of using this variable.
212
self._deprecation_version = deprecation_version
213
self._variable_name = variable_name
214
self._advice = advice
215
dict.__init__(self, initial_value)
217
# This isn't every possible method but it should trap anyone using the
218
# dict -- add more if desired
219
__len__ = _dict_deprecation_wrapper(dict.__len__)
220
__getitem__ = _dict_deprecation_wrapper(dict.__getitem__)
221
__setitem__ = _dict_deprecation_wrapper(dict.__setitem__)
222
__delitem__ = _dict_deprecation_wrapper(dict.__delitem__)
223
keys = _dict_deprecation_wrapper(dict.keys)
224
__contains__ = _dict_deprecation_wrapper(dict.__contains__)
227
def deprecated_list(deprecation_version, variable_name,
228
initial_value, extra=None):
229
"""Create a list that warns when modified
231
:param deprecation_version: something like zero_nine
232
:param initial_value: The contents of the list
233
:param variable_name: This allows better warnings to be printed
234
:param extra: Extra info to print when printing a warning
237
subst_text = 'Modifying %s' % (variable_name,)
238
msg = deprecation_version % (subst_text,)
242
class _DeprecatedList(list):
243
__doc__ = list.__doc__ + msg
247
def _warn_deprecated(self, func, *args, **kwargs):
248
warn(msg, DeprecationWarning, stacklevel=3)
249
return func(self, *args, **kwargs)
251
def append(self, obj):
252
"""appending to %s is deprecated""" % (variable_name,)
253
return self._warn_deprecated(list.append, obj)
255
def insert(self, index, obj):
256
"""inserting to %s is deprecated""" % (variable_name,)
257
return self._warn_deprecated(list.insert, index, obj)
259
def extend(self, iterable):
260
"""extending %s is deprecated""" % (variable_name,)
261
return self._warn_deprecated(list.extend, iterable)
263
def remove(self, value):
264
"""removing from %s is deprecated""" % (variable_name,)
265
return self._warn_deprecated(list.remove, value)
267
def pop(self, index=None):
268
"""pop'ing from from %s is deprecated""" % (variable_name,)
270
return self._warn_deprecated(list.pop, index)
273
return self._warn_deprecated(list.pop)
275
return _DeprecatedList(initial_value)