Skip to content

Commit

Permalink
DOC: Add role for custom informal types like color
Browse files Browse the repository at this point in the history
The role is intended to be used for type references in
docstrings like
```
Parameters
----------
    color : :mpltype:`color`
```

It is easily extendable to other types.

The naming `mpltype` was a quick choice. I'm open to better names.

This PR contains one example usage in `widgets.Button` so that one can
see the effect in the built docs. Systematic application
throughout the codebase should be deferred to a separate PR.

Closes matplotlib#24859.
Formalizes matplotlib#27164.
  • Loading branch information
timhoffm committed Oct 25, 2023
1 parent ecb3c51 commit 689ab84
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
15 changes: 15 additions & 0 deletions doc/sphinxext/custom_roles.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,23 @@ def rcparam_role(name, rawtext, text, lineno, inliner, options={}, content=[]):
return node_list, messages


def mpltype_role(name, rawtext, text, lineno, inliner, options={}, content=[]):
mpltype = text
type_to_link_target = {
'color': 'colors_def',
}
if mpltype not in type_to_link_target:
raise ValueError(f"Unknown mpltype: {mpltype!r}")

ref_nodes, messages = inliner.interpreted(
mpltype, f'{mpltype} <{type_to_link_target[mpltype]}>', 'ref', lineno)
node_list = [ref_nodes]
return node_list, messages


def setup(app):
app.add_role("rc", rcparam_role)
app.add_role("mpltype", mpltype_role)
app.add_node(
QueryReference,
html=(visit_query_reference_node, depart_query_reference_node),
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ def __init__(self, ax, label, image=None,
image : array-like or PIL Image
The image to place in the button, if not *None*. The parameter is
directly forwarded to `~.axes.Axes.imshow`.
color : color
color : :mpltype:`color`
The color of the button when not activated.
hovercolor : color
The color of the button when the mouse is over it.
Expand Down

0 comments on commit 689ab84

Please sign in to comment.