deprecation.py 20 KB

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