176
176
decorated_callable.is_deprecated = True
179
def _dict_deprecation_wrapper(func):
180
"""Returns a closure that emits a warning and calls the superclass"""
181
def cb(dep_dict, *args, **kwargs):
182
msg = 'access to %s' % (dep_dict._variable_name, )
183
msg = dep_dict._deprecation_version % (msg,)
185
msg += ' ' + dep_dict._advice
186
warn(msg, DeprecationWarning, stacklevel=2)
187
return func(dep_dict, *args, **kwargs)
191
class DeprecatedDict(dict):
192
"""A dictionary that complains when read or written."""
202
"""Create a dict that warns when read or modified.
204
:param deprecation_version: something like zero_nine
205
:param initial_value: The contents of the dict
206
:param variable_name: This allows better warnings to be printed
207
:param advice: String of advice on what callers should do instead
208
of using this variable.
210
self._deprecation_version = deprecation_version
211
self._variable_name = variable_name
212
self._advice = advice
213
dict.__init__(self, initial_value)
215
# This isn't every possible method but it should trap anyone using the
216
# dict -- add more if desired
217
__len__ = _dict_deprecation_wrapper(dict.__len__)
218
__getitem__ = _dict_deprecation_wrapper(dict.__getitem__)
219
__setitem__ = _dict_deprecation_wrapper(dict.__setitem__)
220
__delitem__ = _dict_deprecation_wrapper(dict.__delitem__)
221
keys = _dict_deprecation_wrapper(dict.keys)
222
__contains__ = _dict_deprecation_wrapper(dict.__contains__)
179
225
def deprecated_list(deprecation_version, variable_name,
180
226
initial_value, extra=None):
181
227
"""Create a list that warns when modified