Skip to content

Commit a61274d

Browse files
committed
Copy docstring via decorator
... to play nicely with deprecation warnings in docstrings
1 parent 31b4340 commit a61274d

3 files changed

Lines changed: 44 additions & 3 deletions

File tree

lib/matplotlib/_api/__init__.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,3 +423,43 @@ def warn_external(message, category=None):
423423
# preemptively break reference cycle between locals and the frame
424424
del frame
425425
warnings.warn(message, category, **kwargs)
426+
427+
428+
429+
def copy_docstring(source):
430+
"""
431+
A decorator to copy a docstring from another function.
432+
433+
Parameters
434+
----------
435+
source : callable
436+
The function from which to copy the docstring.
437+
438+
Examples
439+
--------
440+
::
441+
442+
@copy_docstring(source=source_func)
443+
def my_func():
444+
...
445+
446+
Notes
447+
-----
448+
This is particularly useful when making a function private and providing
449+
a backward-compatible public wrapper. You can simply prefix the existing
450+
function with an underscore and create the backward-compatible public
451+
wrapper as ::
452+
453+
@_api.deprecated("3.11")
454+
@_api.copy_docstring(source=source_func)
455+
def my_func():
456+
...
457+
458+
This ensures that the docstring is present and amended with the deprecation
459+
note.
460+
"""
461+
def decorator(func):
462+
func.__doc__ = source.__doc__
463+
return func
464+
465+
return decorator

lib/matplotlib/_api/__init__.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,4 @@ def recursive_subclasses(cls: type) -> Generator[type, None, None]: ...
6161
def warn_external(
6262
message: str | Warning, category: type[Warning] | None = ...
6363
) -> None: ...
64+
def copy_docstring(func: Callable[..., Any]) -> Callable[..., Any]: ...

lib/matplotlib/transforms.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2924,9 +2924,9 @@ def _nonsingular(vmin, vmax, expander=0.001, tiny=1e-15, increasing=True):
29242924

29252925

29262926
@_api.deprecated("3.11")
2927+
@_api.copy_docstring(_nonsingular)
29272928
def nonsingular(vmin, vmax, expander=0.001, tiny=1e-15, increasing=True):
29282929
return _nonsingular(vmin, vmax, expander, tiny, increasing)
2929-
nonsingular.__doc__ = _nonsingular.__doc__
29302930

29312931

29322932
def _interval_contains(interval, val):
@@ -2952,9 +2952,9 @@ def _interval_contains(interval, val):
29522952

29532953

29542954
@_api.deprecated("3.11")
2955+
@_api.copy_docstring(_interval_contains)
29552956
def interval_contains(interval, val):
29562957
return _interval_contains(interval, val)
2957-
interval_contains.__doc__ = _interval_contains.__doc__
29582958

29592959

29602960
def _interval_contains_close(interval, val, rtol=1e-10):
@@ -3007,9 +3007,9 @@ def _interval_contains_open(interval, val):
30073007

30083008

30093009
@_api.deprecated("3.11")
3010+
@_api.copy_docstring(_interval_contains_open)
30103011
def interval_contains_open(interval, val):
30113012
return _interval_contains_open(interval, val)
3012-
_interval_contains_open.__doc__ = _interval_contains_open.__doc__
30133013

30143014

30153015
def offset_copy(trans, fig=None, x=0.0, y=0.0, units='inches'):

0 commit comments

Comments
 (0)