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

Skip to content

Commit d47f986

Browse files
timhoffmoscargus
authored andcommitted
Proof of concept for adding kwdoc content to properties using a decorator
1 parent 61ed3f4 commit d47f986

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

lib/matplotlib/_docstring.py

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

55

6+
def kwarg_doc(text):
7+
"""
8+
Decorator for artist property setters, defining the documentation used when
9+
artist properties are passed through as keyword arguments.
10+
11+
We generate lists of kwargs by inspecting the properties on a class, and then refer
12+
to them in the documentation as, for example, ``%(Text:kwdoc)s``, which is used in
13+
the `**kwargs` section in `.Axes.text`. However, sometimes the setter function
14+
parameter description is not what we want in this generated list, or we cannot pick
15+
up the correct description.
16+
17+
This method adds the given text to a private variable ``_kwarg_doc`` on the class.
18+
It will be used for generating the kwdoc list for artists instead of the parameter
19+
docs.
20+
21+
The text should contain the supported types as well as the default value
22+
if applicable, e.g.:
23+
24+
@_docstring.kwarg_doc("bool, default: :rc:`text.usetex`")
25+
def set_usetex(self, usetex):
26+
27+
"""
28+
def decorator(func):
29+
func._kwarg_doc = text
30+
return func
31+
return decorator
32+
33+
634
class Substitution:
735
"""
836
A decorator that performs %-substitution on an object's docstring.

lib/matplotlib/artist.py

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

1482+
if hasattr(func, '_kwarg_doc'):
1483+
return func._kwarg_doc
1484+
14821485
docstring = inspect.getdoc(func)
14831486
if docstring is None:
14841487
return 'unknown'

lib/matplotlib/text.py

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

1285+
@_docstring.kwarg_doc("bool, default: :rc:`text.usetex`")
12851286
def set_usetex(self, usetex):
12861287
"""
12871288
Parameters

0 commit comments

Comments
 (0)