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

Skip to content

Commit cfd284f

Browse files
committed
DOC: Add role for custom informal types like color
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 #24859. Formalizes #27164.
1 parent ecb3c51 commit cfd284f

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

doc/sphinxext/custom_roles.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from urllib.parse import urlsplit, urlunsplit
22

33
from docutils import nodes
4+
from sphinx.util.nodes import make_refnode
45

56
from matplotlib import rcParamsDefault
67

@@ -64,8 +65,23 @@ def rcparam_role(name, rawtext, text, lineno, inliner, options={}, content=[]):
6465
return node_list, messages
6566

6667

68+
def mpltype_role(name, rawtext, text, lineno, inliner, options={}, content=[]):
69+
mpltype = text
70+
type_to_link_target = {
71+
'color': 'colors_def',
72+
}
73+
if mpltype not in type_to_link_target:
74+
raise ValueError(f"Unknown mpltype: {mpltype!r}")
75+
76+
ref_nodes, messages = inliner.interpreted(
77+
mpltype, f'{mpltype} <{type_to_link_target[mpltype]}>', 'ref', lineno)
78+
node_list = [ref_nodes]
79+
return node_list, messages
80+
81+
6782
def setup(app):
6883
app.add_role("rc", rcparam_role)
84+
app.add_role("mpltype", mpltype_role)
6985
app.add_node(
7086
QueryReference,
7187
html=(visit_query_reference_node, depart_query_reference_node),

lib/matplotlib/widgets.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ def __init__(self, ax, label, image=None,
191191
image : array-like or PIL Image
192192
The image to place in the button, if not *None*. The parameter is
193193
directly forwarded to `~.axes.Axes.imshow`.
194-
color : color
194+
color : :mpltype:`color`
195195
The color of the button when not activated.
196196
hovercolor : color
197197
The color of the button when the mouse is over it.

0 commit comments

Comments
 (0)