Skip to content

Commit e8f237e

Browse files
committed
WIP: kwarg to ignore unknown terms in make_xref.
Add a kwarg to make_xref to toggle the automatic wrapping of every term not in xref_ignore in an :obj: role.
1 parent 676a8d4 commit e8f237e

File tree

2 files changed

+107
-3
lines changed

2 files changed

+107
-3
lines changed

numpydoc/tests/test_xref.py

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,101 @@
102102
:class:`python:dict`\[:class:`python:tuple`\(:class:`python:str`, :class:`python:str`), :class:`python:int`]
103103
""" # noqa: E501
104104

105+
data_ignore_obj = r"""
106+
(...) array_like, float, optional
107+
(...) :term:`numpy:array_like`, :class:`python:float`, optional
108+
109+
(2,) ndarray
110+
(2,) :obj:`ndarray <numpy.ndarray>`
111+
112+
(...,M,N) array_like
113+
(...,M,N) :term:`numpy:array_like`
114+
115+
(..., M, N) array_like
116+
(..., M, N) :term:`numpy:array_like`
117+
118+
(float, float), optional
119+
(:class:`python:float`, :class:`python:float`), optional
120+
121+
1-D array or sequence
122+
1-D :obj:`array <numpy.ndarray>` or :term:`python:sequence`
123+
124+
array of str or unicode-like
125+
:obj:`array <numpy.ndarray>` of :class:`python:str` or unicode-like
126+
127+
array_like of float
128+
:term:`numpy:array_like` of :class:`python:float`
129+
130+
bool or callable
131+
:ref:`bool <python:bltin-boolean-values>` or :func:`python:callable`
132+
133+
int in [0, 255]
134+
:class:`python:int` in [0, 255]
135+
136+
int or None, optional
137+
:class:`python:int` or :data:`python:None`, optional
138+
139+
list of str or array_like
140+
:class:`python:list` of :class:`python:str` or :term:`numpy:array_like`
141+
142+
sequence of array_like
143+
:term:`python:sequence` of :term:`numpy:array_like`
144+
145+
str or pathlib.Path
146+
:class:`python:str` or pathlib.Path
147+
148+
{'', string}, optional
149+
{'', :class:`python:str`}, optional
150+
151+
{'C', 'F', 'A', or 'K'}, optional
152+
{'C', 'F', 'A', or 'K'}, optional
153+
154+
{'linear', 'lower', 'higher', 'midpoint', 'nearest'}
155+
{'linear', 'lower', 'higher', 'midpoint', 'nearest'}
156+
157+
{False, True, 'greedy', 'optimal'}
158+
{:data:`python:False`, :data:`python:True`, 'greedy', 'optimal'}
159+
160+
{{'begin', 1}, {'end', 0}}, {string, int}
161+
{{'begin', 1}, {'end', 0}}, {:class:`python:str`, :class:`python:int`}
162+
163+
callable f'(x,*args)
164+
:func:`python:callable` f'(x,*args)
165+
166+
callable ``fhess(x, *args)``, optional
167+
:func:`python:callable` ``fhess(x, *args)``, optional
168+
169+
spmatrix (format: ``csr``, ``bsr``, ``dia`` or coo``)
170+
spmatrix (format: ``csr``, ``bsr``, ``dia`` or coo``)
171+
172+
:ref:`strftime <strftime-strptime-behavior>`
173+
:ref:`strftime <strftime-strptime-behavior>`
174+
175+
callable or :ref:`strftime <strftime-strptime-behavior>`
176+
:func:`python:callable` or :ref:`strftime <strftime-strptime-behavior>`
177+
178+
callable or :ref:`strftime behavior <strftime-strptime-behavior>`
179+
:func:`python:callable` or :ref:`strftime behavior <strftime-strptime-behavior>`
180+
181+
list(int)
182+
:class:`python:list`\(:class:`python:int`)
183+
184+
list[int]
185+
:class:`python:list`\[:class:`python:int`]
186+
187+
dict(str, int)
188+
:class:`python:dict`\(:class:`python:str`, :class:`python:int`)
189+
190+
dict[str, int]
191+
:class:`python:dict`\[:class:`python:str`, :class:`python:int`]
192+
193+
tuple(float, float)
194+
:class:`python:tuple`\(:class:`python:float`, :class:`python:float`)
195+
196+
dict[tuple(str, str), int]
197+
:class:`python:dict`\[:class:`python:tuple`\(:class:`python:str`, :class:`python:str`), :class:`python:int`]
198+
""" # noqa: E501
199+
105200
xref_ignore = {'or', 'in', 'of', 'default', 'optional'}
106201

107202

@@ -111,3 +206,10 @@
111206
)
112207
def test_make_xref(param_type, expected_result):
113208
assert make_xref(param_type, xref_aliases, xref_ignore) == expected_result
209+
210+
@pytest.mark.parametrize(
211+
('param_type', 'expected_result'),
212+
[tuple(s.split('\n')) for s in data_ignore_obj.strip().split('\n\n')]
213+
)
214+
def test_make_xref_ignore_unknown(param_type, expected_result):
215+
assert make_xref(param_type, xref_aliases, xref_ignore, wrap_unknown=False) == expected_result

numpydoc/xref.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@
9494
}
9595

9696

97-
def make_xref(param_type, xref_aliases, xref_ignore):
97+
def make_xref(param_type, xref_aliases, xref_ignore, wrap_unknown=True):
9898
"""Parse and apply appropriate sphinx role(s) to `param_type`.
9999
100100
The :obj: role is the default.
@@ -124,8 +124,9 @@ def make_xref(param_type, xref_aliases, xref_ignore):
124124
if QUALIFIED_NAME_RE.match(link) and link not in xref_ignore:
125125
if link != title:
126126
return ':obj:`%s <%s>`' % (title, link)
127-
else:
127+
if wrap_unknown:
128128
return ':obj:`%s`' % link
129+
return link
129130

130131
def _split_and_apply_re(s, pattern):
131132
"""
@@ -142,7 +143,8 @@ def _split_and_apply_re(s, pattern):
142143
results.append(tok)
143144
else:
144145
res = make_xref(
145-
tok, xref_aliases, xref_ignore)
146+
tok, xref_aliases, xref_ignore, wrap_unknown
147+
)
146148
# Opening brackets immediately after a role is
147149
# bad markup. Detect that and add backslash.
148150
# :role:`type`( to :role:`type`\(

0 commit comments

Comments
 (0)