Skip to content

Proof of concept for adding kwdoc content to properties using a decorator #22699

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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