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

Skip to content

Commit a7a0a67

Browse files
committed
Proof of concept for adding kwdoc content to properties using a decorator
1 parent 2e95cf2 commit a7a0a67

File tree

4 files changed

+32
-0
lines changed

4 files changed

+32
-0
lines changed

lib/matplotlib/_docstring.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,31 @@
33
from . import _api
44

55

6+
def kwarg_doc(text):
7+
"""
8+
Decorator for defining the documentation used when artist properties are
9+
passed through as keyword arguments.
10+
11+
See e.g. the `**kwargs` section in `.Axes.text`.
12+
13+
The given text is stored in a privat variable ``_kwarg_doc`` on the method.
14+
It is used for generating the kwdoc list for artists, which we
15+
auto-generate through docstring interpolation, e.g. via
16+
``%(Line2D:kwdoc)s``.
17+
18+
The text should contain the supported types as well as the default value
19+
if applicable, e.g.:
20+
21+
@_docstring.kwarg_doc("bool, default: :rc:`text.usetex`")
22+
def set_usetex(self, usetex):
23+
24+
"""
25+
def decorator(func):
26+
func._kwarg_doc = text
27+
return func
28+
return decorator
29+
30+
631
class Substitution:
732
"""
833
A decorator that performs %-substitution on an object's docstring.

lib/matplotlib/_docstring.pyi

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ from typing import Any, Callable, TypeVar, overload
44
_T = TypeVar('_T')
55

66

7+
def _kwarg_doc(text: str) -> Callable[[_T], _T]: ...
8+
9+
710
class Substitution:
811
@overload
912
def __init__(self, *args: str): ...

lib/matplotlib/artist.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1482,6 +1482,9 @@ def get_valid_values(self, attr):
14821482
raise AttributeError(f'{self.o} has no function {name}')
14831483
func = getattr(self.o, name)
14841484

1485+
if hasattr(func, '_kwarg_doc'):
1486+
return func._kwarg_doc
1487+
14851488
docstring = inspect.getdoc(func)
14861489
if docstring is None:
14871490
return 'unknown'

lib/matplotlib/text.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1314,6 +1314,7 @@ def set_fontproperties(self, fp):
13141314
self._fontproperties = FontProperties._from_any(fp).copy()
13151315
self.stale = True
13161316

1317+
@_docstring.kwarg_doc("bool, default: :rc:`text.usetex`")
13171318
def set_usetex(self, usetex):
13181319
"""
13191320
Parameters

0 commit comments

Comments
 (0)