Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 22005d7

Browse files
committed
Include alternatives to deprecations in the documentation
- add `alternative` and `addendum` to the documentation by including them in the second argument of the `.. deprecated::` directive - allow for backticks and optional reference to create links to the mentioned alternatives in the documentation - remove these additional links and backticks in the deprecation warning message that is emitted when using a deprecated item Example: @_api.deprecated("3.5", alternative="`num2date(e).timestamp() <.num2date>`" will create the directive .. deprecated:: 3.5 Use `num2date(e).timestamp() <.num2date>` instead. which creates a link to matplotlib.dates.num2date with the caption num2date(e).timestamp(). The deprecation warning message will read "... Use num2date(e).timestamp() instead."
1 parent ec99c15 commit 22005d7

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

doc/devel/contributing.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,14 @@ Introducing
326326
All these helpers take a first parameter *since*, which should be set to
327327
the next point release, e.g. "3.x".
328328

329+
When providing alternatives in ``_api.warn_deprecated()`` or
330+
``@_api.deprecated``, you can use backticks to generate links to these
331+
alternatives in the documentation. If the link differs from the caption to
332+
be shown in the documentation, it can be added in angle brackets after a
333+
space. Backticks including an optional leading dot are stripped from the
334+
alternative and references are replaced by their caption (if any) when
335+
emitting the deprecation warning.
336+
329337
Expiring
330338
~~~~~~~~
331339

lib/matplotlib/_api/deprecation.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import functools
1515
import inspect
1616
import math
17+
import re
1718
import warnings
1819

1920

@@ -47,6 +48,8 @@ def _generate_deprecation_warning(
4748
+ (" %(addendum)s" if addendum else ""))
4849
warning_cls = (PendingDeprecationWarning if pending
4950
else MatplotlibDeprecationWarning)
51+
# remove backticks, optional leading dot and replace reference by caption
52+
alternative = re.sub(r"`([^`]+) <.*>`|`\.?([^`]+)`", r"\1\2", alternative)
5053
return warning_cls(message % dict(
5154
func=name, name=name, obj_type=obj_type, since=since, removal=removal,
5255
alternative=alternative, addendum=addendum))
@@ -207,10 +210,13 @@ def wrapper(*args, **kwargs):
207210
old_doc = inspect.cleandoc(old_doc or '').strip('\n')
208211

209212
notes_header = '\nNotes\n-----'
213+
second_arg = ' '.join([t.strip() for t in
214+
(message, f"Use {alternative} instead."
215+
if alternative else "", addendum) if t])
210216
new_doc = (f"[*Deprecated*] {old_doc}\n"
211217
f"{notes_header if notes_header not in old_doc else ''}\n"
212218
f".. deprecated:: {since}\n"
213-
f" {message.strip()}")
219+
f" {second_arg}")
214220

215221
if not old_doc:
216222
# This is to prevent a spurious 'unexpected unindent' warning from

lib/matplotlib/tests/test_api.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,13 @@ def func(pre, arg, post=None):
8585
func(1, 2)
8686
with pytest.warns(_api.MatplotlibDeprecationWarning):
8787
func(1, 2, 3)
88+
89+
90+
def test_deprecation_alternative():
91+
@_api.deprecated("1", alternative="`f1(arg) <.f1>` or `.f2` or `f3`")
92+
def f():
93+
pass
94+
assert "`f1(arg) <.f1>` or `.f2` or `f3`" in f.__doc__
95+
with pytest.warns(_api.MatplotlibDeprecationWarning,
96+
match=r".* f1\(arg\) or f2 or f3"):
97+
f()

0 commit comments

Comments
 (0)