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

Skip to content

Commit adc0274

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 68d6b79 commit adc0274

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

doc/devel/contributing.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,10 @@ 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+
You can use standard rst cross references in the alternative. The string
330+
will be processed to remove back ticks and only include the link caption
331+
in the runtime warning.
332+
329333
Expiring
330334
~~~~~~~~
331335

lib/matplotlib/_api/deprecation.py

Lines changed: 9 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,10 @@ 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+
if alternative:
53+
alternative = re.sub(r"`([^`]*?) *<.*?>`|`\.?(.+?)`", r"\1\2",
54+
alternative)
5055
return warning_cls(message % dict(
5156
func=name, name=name, obj_type=obj_type, since=since, removal=removal,
5257
alternative=alternative, addendum=addendum))
@@ -207,10 +212,13 @@ def wrapper(*args, **kwargs):
207212
old_doc = inspect.cleandoc(old_doc or '').strip('\n')
208213

209214
notes_header = '\nNotes\n-----'
215+
second_arg = ' '.join([t.strip() for t in
216+
(message, f"Use {alternative} instead."
217+
if alternative else "", addendum) if t])
210218
new_doc = (f"[*Deprecated*] {old_doc}\n"
211219
f"{notes_header if notes_header not in old_doc else ''}\n"
212220
f".. deprecated:: {since}\n"
213-
f" {message.strip()}")
221+
f" {second_arg}")
214222

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

lib/matplotlib/tests/test_api.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,14 @@ 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+
alternative = "`.f1`, `f2`, `f3(x) <.f3>` or `f4(x)<f4>`"
92+
@_api.deprecated("1", alternative=alternative)
93+
def f():
94+
pass
95+
assert alternative in f.__doc__
96+
with pytest.warns(_api.MatplotlibDeprecationWarning,
97+
match=r".* f1, f2, f3\(x\) or f4\(x\)"):
98+
f()

0 commit comments

Comments
 (0)