deprecation.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. """
  2. Helper functions for deprecating parts of the Matplotlib API.
  3. This documentation is only relevant for Matplotlib developers, not for users.
  4. .. warning::
  5. This module is for internal use only. Do not use it in your own code.
  6. We may change the API at any time with no warning.
  7. """
  8. import contextlib
  9. import functools
  10. import inspect
  11. import math
  12. import warnings
  13. class MatplotlibDeprecationWarning(DeprecationWarning):
  14. """A class for issuing deprecation warnings for Matplotlib users."""
  15. def _generate_deprecation_warning(
  16. since, message='', name='', alternative='', pending=False, obj_type='',
  17. addendum='', *, removal=''):
  18. if pending:
  19. if removal:
  20. raise ValueError("A pending deprecation cannot have a scheduled removal")
  21. elif removal == '':
  22. macro, meso, *_ = since.split('.')
  23. removal = f'{macro}.{int(meso) + 2}'
  24. if not message:
  25. message = (
  26. ("The %(name)s %(obj_type)s" if obj_type else "%(name)s") +
  27. (" will be deprecated in a future version" if pending else
  28. (" was deprecated in Matplotlib %(since)s" +
  29. (" and will be removed in %(removal)s" if removal else ""))) +
  30. "." +
  31. (" Use %(alternative)s instead." if alternative else "") +
  32. (" %(addendum)s" if addendum else ""))
  33. warning_cls = PendingDeprecationWarning if pending else MatplotlibDeprecationWarning
  34. return warning_cls(message % dict(
  35. func=name, name=name, obj_type=obj_type, since=since, removal=removal,
  36. alternative=alternative, addendum=addendum))
  37. def warn_deprecated(
  38. since, *, message='', name='', alternative='', pending=False,
  39. obj_type='', addendum='', removal=''):
  40. """
  41. Display a standardized deprecation.
  42. Parameters
  43. ----------
  44. since : str
  45. The release at which this API became deprecated.
  46. message : str, optional
  47. Override the default deprecation message. The ``%(since)s``,
  48. ``%(name)s``, ``%(alternative)s``, ``%(obj_type)s``, ``%(addendum)s``,
  49. and ``%(removal)s`` format specifiers will be replaced by the values
  50. of the respective arguments passed to this function.
  51. name : str, optional
  52. The name of the deprecated object.
  53. alternative : str, optional
  54. An alternative API that the user may use in place of the deprecated
  55. API. The deprecation warning will tell the user about this alternative
  56. if provided.
  57. pending : bool, optional
  58. If True, uses a PendingDeprecationWarning instead of a
  59. DeprecationWarning. Cannot be used together with *removal*.
  60. obj_type : str, optional
  61. The object type being deprecated.
  62. addendum : str, optional
  63. Additional text appended directly to the final message.
  64. removal : str, optional
  65. The expected removal version. With the default (an empty string), a
  66. removal version is automatically computed from *since*. Set to other
  67. Falsy values to not schedule a removal date. Cannot be used together
  68. with *pending*.
  69. Examples
  70. --------
  71. ::
  72. # To warn of the deprecation of "matplotlib.name_of_module"
  73. warn_deprecated('1.4.0', name='matplotlib.name_of_module',
  74. obj_type='module')
  75. """
  76. warning = _generate_deprecation_warning(
  77. since, message, name, alternative, pending, obj_type, addendum,
  78. removal=removal)
  79. from . import warn_external
  80. warn_external(warning, category=MatplotlibDeprecationWarning)
  81. def deprecated(since, *, message='', name='', alternative='', pending=False,
  82. obj_type=None, addendum='', removal=''):
  83. """
  84. Decorator to mark a function, a class, or a property as deprecated.
  85. When deprecating a classmethod, a staticmethod, or a property, the
  86. ``@deprecated`` decorator should go *under* ``@classmethod`` and
  87. ``@staticmethod`` (i.e., `deprecated` should directly decorate the
  88. underlying callable), but *over* ``@property``.
  89. When deprecating a class ``C`` intended to be used as a base class in a
  90. multiple inheritance hierarchy, ``C`` *must* define an ``__init__`` method
  91. (if ``C`` instead inherited its ``__init__`` from its own base class, then
  92. ``@deprecated`` would mess up ``__init__`` inheritance when installing its
  93. own (deprecation-emitting) ``C.__init__``).
  94. Parameters are the same as for `warn_deprecated`, except that *obj_type*
  95. defaults to 'class' if decorating a class, 'attribute' if decorating a
  96. property, and 'function' otherwise.
  97. Examples
  98. --------
  99. ::
  100. @deprecated('1.4.0')
  101. def the_function_to_deprecate():
  102. pass
  103. """
  104. def deprecate(obj, message=message, name=name, alternative=alternative,
  105. pending=pending, obj_type=obj_type, addendum=addendum):
  106. from matplotlib._api import classproperty
  107. if isinstance(obj, type):
  108. if obj_type is None:
  109. obj_type = "class"
  110. func = obj.__init__
  111. name = name or obj.__name__
  112. old_doc = obj.__doc__
  113. def finalize(wrapper, new_doc):
  114. try:
  115. obj.__doc__ = new_doc
  116. except AttributeError: # Can't set on some extension objects.
  117. pass
  118. obj.__init__ = functools.wraps(obj.__init__)(wrapper)
  119. return obj
  120. elif isinstance(obj, (property, classproperty)):
  121. if obj_type is None:
  122. obj_type = "attribute"
  123. func = None
  124. name = name or obj.fget.__name__
  125. old_doc = obj.__doc__
  126. class _deprecated_property(type(obj)):
  127. def __get__(self, instance, owner=None):
  128. if instance is not None or owner is not None \
  129. and isinstance(self, classproperty):
  130. emit_warning()
  131. return super().__get__(instance, owner)
  132. def __set__(self, instance, value):
  133. if instance is not None:
  134. emit_warning()
  135. return super().__set__(instance, value)
  136. def __delete__(self, instance):
  137. if instance is not None:
  138. emit_warning()
  139. return super().__delete__(instance)
  140. def __set_name__(self, owner, set_name):
  141. nonlocal name
  142. if name == "<lambda>":
  143. name = set_name
  144. def finalize(_, new_doc):
  145. return _deprecated_property(
  146. fget=obj.fget, fset=obj.fset, fdel=obj.fdel, doc=new_doc)
  147. else:
  148. if obj_type is None:
  149. obj_type = "function"
  150. func = obj
  151. name = name or obj.__name__
  152. old_doc = func.__doc__
  153. def finalize(wrapper, new_doc):
  154. wrapper = functools.wraps(func)(wrapper)
  155. wrapper.__doc__ = new_doc
  156. return wrapper
  157. def emit_warning():
  158. warn_deprecated(
  159. since, message=message, name=name, alternative=alternative,
  160. pending=pending, obj_type=obj_type, addendum=addendum,
  161. removal=removal)
  162. def wrapper(*args, **kwargs):
  163. emit_warning()
  164. return func(*args, **kwargs)
  165. old_doc = inspect.cleandoc(old_doc or '').strip('\n')
  166. notes_header = '\nNotes\n-----'
  167. second_arg = ' '.join([t.strip() for t in
  168. (message, f"Use {alternative} instead."
  169. if alternative else "", addendum) if t])
  170. new_doc = (f"[*Deprecated*] {old_doc}\n"
  171. f"{notes_header if notes_header not in old_doc else ''}\n"
  172. f".. deprecated:: {since}\n"
  173. f" {second_arg}")
  174. if not old_doc:
  175. # This is to prevent a spurious 'unexpected unindent' warning from
  176. # docutils when the original docstring was blank.
  177. new_doc += r'\ '
  178. return finalize(wrapper, new_doc)
  179. return deprecate
  180. class deprecate_privatize_attribute:
  181. """
  182. Helper to deprecate public access to an attribute (or method).
  183. This helper should only be used at class scope, as follows::
  184. class Foo:
  185. attr = _deprecate_privatize_attribute(*args, **kwargs)
  186. where *all* parameters are forwarded to `deprecated`. This form makes
  187. ``attr`` a property which forwards read and write access to ``self._attr``
  188. (same name but with a leading underscore), with a deprecation warning.
  189. Note that the attribute name is derived from *the name this helper is
  190. assigned to*. This helper also works for deprecating methods.
  191. """
  192. def __init__(self, *args, **kwargs):
  193. self.deprecator = deprecated(*args, **kwargs)
  194. def __set_name__(self, owner, name):
  195. setattr(owner, name, self.deprecator(
  196. property(lambda self: getattr(self, f"_{name}"),
  197. lambda self, value: setattr(self, f"_{name}", value)),
  198. name=name))
  199. # Used by _copy_docstring_and_deprecators to redecorate pyplot wrappers and
  200. # boilerplate.py to retrieve original signatures. It may seem natural to store
  201. # this information as an attribute on the wrapper, but if the wrapper gets
  202. # itself functools.wraps()ed, then such attributes are silently propagated to
  203. # the outer wrapper, which is not desired.
  204. DECORATORS = {}
  205. def rename_parameter(since, old, new, func=None):
  206. """
  207. Decorator indicating that parameter *old* of *func* is renamed to *new*.
  208. The actual implementation of *func* should use *new*, not *old*. If *old*
  209. is passed to *func*, a DeprecationWarning is emitted, and its value is
  210. used, even if *new* is also passed by keyword (this is to simplify pyplot
  211. wrapper functions, which always pass *new* explicitly to the Axes method).
  212. If *new* is also passed but positionally, a TypeError will be raised by the
  213. underlying function during argument binding.
  214. Examples
  215. --------
  216. ::
  217. @_api.rename_parameter("3.1", "bad_name", "good_name")
  218. def func(good_name): ...
  219. """
  220. decorator = functools.partial(rename_parameter, since, old, new)
  221. if func is None:
  222. return decorator
  223. signature = inspect.signature(func)
  224. assert old not in signature.parameters, (
  225. f"Matplotlib internal error: {old!r} cannot be a parameter for "
  226. f"{func.__name__}()")
  227. assert new in signature.parameters, (
  228. f"Matplotlib internal error: {new!r} must be a parameter for "
  229. f"{func.__name__}()")
  230. @functools.wraps(func)
  231. def wrapper(*args, **kwargs):
  232. if old in kwargs:
  233. warn_deprecated(
  234. since, message=f"The {old!r} parameter of {func.__name__}() "
  235. f"has been renamed {new!r} since Matplotlib {since}; support "
  236. f"for the old name will be dropped in %(removal)s.")
  237. kwargs[new] = kwargs.pop(old)
  238. return func(*args, **kwargs)
  239. # wrapper() must keep the same documented signature as func(): if we
  240. # instead made both *old* and *new* appear in wrapper()'s signature, they
  241. # would both show up in the pyplot function for an Axes method as well and
  242. # pyplot would explicitly pass both arguments to the Axes method.
  243. DECORATORS[wrapper] = decorator
  244. return wrapper
  245. class _deprecated_parameter_class:
  246. def __repr__(self):
  247. return "<deprecated parameter>"
  248. _deprecated_parameter = _deprecated_parameter_class()
  249. def delete_parameter(since, name, func=None, **kwargs):
  250. """
  251. Decorator indicating that parameter *name* of *func* is being deprecated.
  252. The actual implementation of *func* should keep the *name* parameter in its
  253. signature, or accept a ``**kwargs`` argument (through which *name* would be
  254. passed).
  255. Parameters that come after the deprecated parameter effectively become
  256. keyword-only (as they cannot be passed positionally without triggering the
  257. DeprecationWarning on the deprecated parameter), and should be marked as
  258. such after the deprecation period has passed and the deprecated parameter
  259. is removed.
  260. Parameters other than *since*, *name*, and *func* are keyword-only and
  261. forwarded to `.warn_deprecated`.
  262. Examples
  263. --------
  264. ::
  265. @_api.delete_parameter("3.1", "unused")
  266. def func(used_arg, other_arg, unused, more_args): ...
  267. """
  268. decorator = functools.partial(delete_parameter, since, name, **kwargs)
  269. if func is None:
  270. return decorator
  271. signature = inspect.signature(func)
  272. # Name of `**kwargs` parameter of the decorated function, typically
  273. # "kwargs" if such a parameter exists, or None if the decorated function
  274. # doesn't accept `**kwargs`.
  275. kwargs_name = next((param.name for param in signature.parameters.values()
  276. if param.kind == inspect.Parameter.VAR_KEYWORD), None)
  277. if name in signature.parameters:
  278. kind = signature.parameters[name].kind
  279. is_varargs = kind is inspect.Parameter.VAR_POSITIONAL
  280. is_varkwargs = kind is inspect.Parameter.VAR_KEYWORD
  281. if not is_varargs and not is_varkwargs:
  282. name_idx = (
  283. # Deprecated parameter can't be passed positionally.
  284. math.inf if kind is inspect.Parameter.KEYWORD_ONLY
  285. # If call site has no more than this number of parameters, the
  286. # deprecated parameter can't have been passed positionally.
  287. else [*signature.parameters].index(name))
  288. func.__signature__ = signature = signature.replace(parameters=[
  289. param.replace(default=_deprecated_parameter)
  290. if param.name == name else param
  291. for param in signature.parameters.values()])
  292. else:
  293. name_idx = -1 # Deprecated parameter can always have been passed.
  294. else:
  295. is_varargs = is_varkwargs = False
  296. # Deprecated parameter can't be passed positionally.
  297. name_idx = math.inf
  298. assert kwargs_name, (
  299. f"Matplotlib internal error: {name!r} must be a parameter for "
  300. f"{func.__name__}()")
  301. addendum = kwargs.pop('addendum', None)
  302. @functools.wraps(func)
  303. def wrapper(*inner_args, **inner_kwargs):
  304. if len(inner_args) <= name_idx and name not in inner_kwargs:
  305. # Early return in the simple, non-deprecated case (much faster than
  306. # calling bind()).
  307. return func(*inner_args, **inner_kwargs)
  308. arguments = signature.bind(*inner_args, **inner_kwargs).arguments
  309. if is_varargs and arguments.get(name):
  310. warn_deprecated(
  311. since, message=f"Additional positional arguments to "
  312. f"{func.__name__}() are deprecated since %(since)s and "
  313. f"support for them will be removed in %(removal)s.")
  314. elif is_varkwargs and arguments.get(name):
  315. warn_deprecated(
  316. since, message=f"Additional keyword arguments to "
  317. f"{func.__name__}() are deprecated since %(since)s and "
  318. f"support for them will be removed in %(removal)s.")
  319. # We cannot just check `name not in arguments` because the pyplot
  320. # wrappers always pass all arguments explicitly.
  321. elif any(name in d and d[name] != _deprecated_parameter
  322. for d in [arguments, arguments.get(kwargs_name, {})]):
  323. deprecation_addendum = (
  324. f"If any parameter follows {name!r}, they should be passed as "
  325. f"keyword, not positionally.")
  326. warn_deprecated(
  327. since,
  328. name=repr(name),
  329. obj_type=f"parameter of {func.__name__}()",
  330. addendum=(addendum + " " + deprecation_addendum) if addendum
  331. else deprecation_addendum,
  332. **kwargs)
  333. return func(*inner_args, **inner_kwargs)
  334. DECORATORS[wrapper] = decorator
  335. return wrapper
  336. def make_keyword_only(since, name, func=None):
  337. """
  338. Decorator indicating that passing parameter *name* (or any of the following
  339. ones) positionally to *func* is being deprecated.
  340. When used on a method that has a pyplot wrapper, this should be the
  341. outermost decorator, so that :file:`boilerplate.py` can access the original
  342. signature.
  343. """
  344. decorator = functools.partial(make_keyword_only, since, name)
  345. if func is None:
  346. return decorator
  347. signature = inspect.signature(func)
  348. POK = inspect.Parameter.POSITIONAL_OR_KEYWORD
  349. KWO = inspect.Parameter.KEYWORD_ONLY
  350. assert (name in signature.parameters
  351. and signature.parameters[name].kind == POK), (
  352. f"Matplotlib internal error: {name!r} must be a positional-or-keyword "
  353. f"parameter for {func.__name__}(). If this error happens on a function with a "
  354. f"pyplot wrapper, make sure make_keyword_only() is the outermost decorator.")
  355. names = [*signature.parameters]
  356. name_idx = names.index(name)
  357. kwonly = [name for name in names[name_idx:]
  358. if signature.parameters[name].kind == POK]
  359. @functools.wraps(func)
  360. def wrapper(*args, **kwargs):
  361. # Don't use signature.bind here, as it would fail when stacked with
  362. # rename_parameter and an "old" argument name is passed in
  363. # (signature.bind would fail, but the actual call would succeed).
  364. if len(args) > name_idx:
  365. warn_deprecated(
  366. since, message="Passing the %(name)s %(obj_type)s "
  367. "positionally is deprecated since Matplotlib %(since)s; the "
  368. "parameter will become keyword-only in %(removal)s.",
  369. name=name, obj_type=f"parameter of {func.__name__}()")
  370. return func(*args, **kwargs)
  371. # Don't modify *func*'s signature, as boilerplate.py needs it.
  372. wrapper.__signature__ = signature.replace(parameters=[
  373. param.replace(kind=KWO) if param.name in kwonly else param
  374. for param in signature.parameters.values()])
  375. DECORATORS[wrapper] = decorator
  376. return wrapper
  377. def deprecate_method_override(method, obj, *, allow_empty=False, **kwargs):
  378. """
  379. Return ``obj.method`` with a deprecation if it was overridden, else None.
  380. Parameters
  381. ----------
  382. method
  383. An unbound method, i.e. an expression of the form
  384. ``Class.method_name``. Remember that within the body of a method, one
  385. can always use ``__class__`` to refer to the class that is currently
  386. being defined.
  387. obj
  388. Either an object of the class where *method* is defined, or a subclass
  389. of that class.
  390. allow_empty : bool, default: False
  391. Whether to allow overrides by "empty" methods without emitting a
  392. warning.
  393. **kwargs
  394. Additional parameters passed to `warn_deprecated` to generate the
  395. deprecation warning; must at least include the "since" key.
  396. """
  397. def empty(): pass
  398. def empty_with_docstring(): """doc"""
  399. name = method.__name__
  400. bound_child = getattr(obj, name)
  401. bound_base = (
  402. method # If obj is a class, then we need to use unbound methods.
  403. if isinstance(bound_child, type(empty)) and isinstance(obj, type)
  404. else method.__get__(obj))
  405. if (bound_child != bound_base
  406. and (not allow_empty
  407. or (getattr(getattr(bound_child, "__code__", None),
  408. "co_code", None)
  409. not in [empty.__code__.co_code,
  410. empty_with_docstring.__code__.co_code]))):
  411. warn_deprecated(**{"name": name, "obj_type": "method", **kwargs})
  412. return bound_child
  413. return None
  414. @contextlib.contextmanager
  415. def suppress_matplotlib_deprecation_warning():
  416. with warnings.catch_warnings():
  417. warnings.simplefilter("ignore", MatplotlibDeprecationWarning)
  418. yield