Skip to content

Commit

Permalink
Proof of concept for adding kwdoc content to properties using a decor…
Browse files Browse the repository at this point in the history
…ator
  • Loading branch information
timhoffm committed Dec 21, 2023
1 parent 2e95cf2 commit aaf53c9
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 deletions.
28 changes: 28 additions & 0 deletions lib/matplotlib/_docstring.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,34 @@
from . import _api


def kwarg_doc(text):
"""
Decorator for defining the kwdoc documentation of artist properties.
This decorator can be applied to artist property setter methods.
The given text is stored in a privat attribute ``_kwarg_doc`` on
the method. It is used to overwrite auto-generated documentation
in the *kwdoc list* for artists. The kwdoc list is used to document
``**kwargs`` when they are properties of an artist. See e.g. the
``**kwargs`` section in `.Axes.text`.
The text should contain the supported types as well as the default value
if applicable, e.g.:
@_docstring.kwarg_doc("bool, default: :rc:`text.usetex`")
def set_usetex(self, usetex):
See Also
--------
matplotlib.artist.kwdoc
"""
def decorator(func):
func._kwarg_doc = text
return func
return decorator


class Substitution:
"""
A decorator that performs %-substitution on an object's docstring.
Expand Down
3 changes: 3 additions & 0 deletions lib/matplotlib/_docstring.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ from typing import Any, Callable, TypeVar, overload
_T = TypeVar('_T')


def kwarg_doc(text: str) -> Callable[[_T], _T]: ...


class Substitution:
@overload
def __init__(self, *args: str): ...
Expand Down
3 changes: 3 additions & 0 deletions lib/matplotlib/artist.py
Original file line number Diff line number Diff line change
Expand Up @@ -1482,6 +1482,9 @@ def get_valid_values(self, attr):
raise AttributeError(f'{self.o} has no function {name}')
func = getattr(self.o, name)

if hasattr(func, '_kwarg_doc'):
return func._kwarg_doc

docstring = inspect.getdoc(func)
if docstring is None:
return 'unknown'
Expand Down
1 change: 1 addition & 0 deletions lib/matplotlib/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -1314,6 +1314,7 @@ def set_fontproperties(self, fp):
self._fontproperties = FontProperties._from_any(fp).copy()
self.stale = True

@_docstring.kwarg_doc("bool, default: :rc:`text.usetex`")
def set_usetex(self, usetex):
"""
Parameters
Expand Down

0 comments on commit aaf53c9

Please sign in to comment.