Skip to content

Commit f2f1867

Browse files
committed
ALTERNATIVE: overload xref_ignore config value
1 parent 7d5346f commit f2f1867

File tree

6 files changed

+37
-36
lines changed

6 files changed

+37
-36
lines changed

doc/install.rst

+11-15
Original file line numberDiff line numberDiff line change
@@ -76,30 +76,26 @@ numpydoc_xref_aliases : dict
7676

7777
This option depends on the ``numpydoc_xref_param_type`` option
7878
being ``True``.
79-
numpydoc_xref_ignore : set
80-
Words not to cross-reference. Most likely, these are common words
79+
numpydoc_xref_ignore : set or ``"all"``
80+
How to handle terms not in ``numpydoc_xref_aliases`` when
81+
``numpydoc_xref_aliases=True``. The value can either be a ``set``
82+
containing terms to ignore, or ``"all"``. In the former case, the set
83+
contains words not to cross-reference. Most likely, these are common words
8184
used in parameter type descriptions that may be confused for
8285
classes of the same name. For example::
8386

8487
numpydoc_xref_ignore = {'type', 'optional', 'default'}
8588

8689
The default is an empty set.
87-
numpydoc_xref_wrap_all : bool
88-
Whether to wrap unrecognized terms in ``param_type`` in the default
89-
``:obj:`` role. The default is ``True``.
90-
An unrecognized term is one that:
91-
92-
1. Is in neither ``numpydoc_xref_aliases`` nor ``numpydoc_xref_ignore``.
93-
2. Is not already wrapped in a ReST role.
9490

91+
If the ``numpydoc_xref_ignore="all"``, then all unrecognized terms are
92+
ignored, i.e. terms not in ``numpydoc_xref_aliases`` are *not* wrapped in
93+
``:obj:`` roles.
9594
This configuration parameter may be useful if you only want create
96-
cross references for a small set of terms. In this case, including the
95+
cross references for a small number of terms. In this case, including the
9796
desired cross reference mappings in ``numpydoc_xref_aliases`` and setting
98-
``numpydoc_xref_wrap_all = False`` is more convenient than adding all of
99-
the non-linked terms to the ``numpydoc_xref_ignore`` set.
100-
101-
If ``numpydoc_xref_param_type`` is set to ``False``, this config parameter
102-
has no effect.
97+
``numpydoc_xref_ignore="all"`` is more convenient than explicitly listing
98+
terms to ignore in a set.
10399
numpydoc_edit_link : bool
104100
.. deprecated:: edit your HTML template instead
105101

numpydoc/docscrape_sphinx.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ def load_config(self, config):
3030
self.xref_param_type = config.get('xref_param_type', False)
3131
self.xref_aliases = config.get('xref_aliases', dict())
3232
self.xref_ignore = config.get('xref_ignore', set())
33-
self.xref_wrap_all = config.get('xref_wrap_all', True)
3433
self.template = config.get('template', None)
3534
if self.template is None:
3635
template_dirs = [os.path.join(os.path.dirname(__file__), 'templates')]
@@ -78,8 +77,7 @@ def _str_returns(self, name='Returns'):
7877
param_type = make_xref(
7978
param_type,
8079
self.xref_aliases,
81-
self.xref_ignore,
82-
self.xref_wrap_all
80+
self.xref_ignore
8381
)
8482
if param.name:
8583
out += self._str_indent([named_fmt % (param.name.strip(),
@@ -220,8 +218,7 @@ def _str_param_list(self, name, fake_autosummary=False):
220218
param_type = make_xref(
221219
param_type,
222220
self.xref_aliases,
223-
self.xref_ignore,
224-
self.xref_wrap_all
221+
self.xref_ignore
225222
)
226223
parts.append(param_type)
227224
out += self._str_indent([' : '.join(parts)])

numpydoc/numpydoc.py

-2
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,6 @@ def mangle_docstrings(app, what, name, obj, options, lines):
155155
'xref_param_type': app.config.numpydoc_xref_param_type,
156156
'xref_aliases': app.config.numpydoc_xref_aliases_complete,
157157
'xref_ignore': app.config.numpydoc_xref_ignore,
158-
'xref_wrap_all': app.config.numpydoc_xref_wrap_all,
159158
}
160159

161160
cfg.update(options or {})
@@ -255,7 +254,6 @@ def setup(app, get_doc_object_=get_doc_object):
255254
app.add_config_value('numpydoc_xref_param_type', False, True)
256255
app.add_config_value('numpydoc_xref_aliases', dict(), True)
257256
app.add_config_value('numpydoc_xref_ignore', set(), True)
258-
app.add_config_value('numpydoc_xref_wrap_all', True, True)
259257

260258
# Extra mangling domains
261259
app.add_domain(NumpyPythonDomain)

numpydoc/tests/test_numpydoc.py

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ class MockConfig():
1515
numpydoc_xref_aliases = {}
1616
numpydoc_xref_aliases_complete = deepcopy(DEFAULT_LINKS)
1717
numpydoc_xref_ignore = set()
18-
numpydoc_xref_wrap_all = True
1918
templates_path = []
2019
numpydoc_edit_link = False
2120
numpydoc_citation_re = '[a-z0-9_.-]+'

numpydoc/tests/test_xref.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -212,4 +212,8 @@ def test_make_xref(param_type, expected_result):
212212
[tuple(s.split('\n')) for s in data_ignore_obj.strip().split('\n\n')]
213213
)
214214
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
215+
assert make_xref(param_type, xref_aliases, xref_ignore="all") == expected_result
216+
217+
def test_xref_ignore_is_all():
218+
with pytest.raises(TypeError, match="must be a set or 'all'"):
219+
make_xref("array_like", xref_aliases, xref_ignore="foo")

numpydoc/xref.py

+19-12
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, wrap_unknown=True):
97+
def make_xref(param_type, xref_aliases, xref_ignore):
9898
"""Parse and apply appropriate sphinx role(s) to `param_type`.
9999
100100
The :obj: role is the default.
@@ -106,27 +106,36 @@ def make_xref(param_type, xref_aliases, xref_ignore, wrap_unknown=True):
106106
xref_aliases : dict
107107
Mapping used to resolve common abbreviations and aliases
108108
to fully qualified names that can be cross-referenced.
109-
xref_ignore : set
110-
Words not to cross-reference.
111-
wrap_unknown : bool, default=True
112-
Toggle whether to wrap unrecognized terms in the default :obj: role,
113-
default is `True`. Unrecognized terms include those that are in
114-
neither `xref_aliases` nor `xref_ignore` and are not already wrapped
115-
in an rST role.
109+
xref_ignore : set or "all"
110+
A set containing words not to cross-reference. Instead of a set, the
111+
string 'all' can be given to ignore all unrecognized terms.
112+
Unrecognized terms include those that are not in `xref_aliases` and
113+
are not already wrapped in a reST role.
116114
117115
Returns
118116
-------
119117
out : str
120118
Text with fully-qualified names and terms that may be wrapped in a
121119
``:obj:`` role.
122120
"""
121+
ignore_set = xref_ignore
122+
wrap_unknown = True
123+
if isinstance(xref_ignore, str):
124+
if xref_ignore.lower() == "all":
125+
wrap_unknown = False
126+
ignore_set = set()
127+
else:
128+
raise TypeError(
129+
"xref_ignore must be a set or 'all', got {}".format(xref_ignore)
130+
)
131+
123132
if param_type in xref_aliases:
124133
link, title = xref_aliases[param_type], param_type
125134
param_type = link
126135
else:
127136
link = title = param_type
128137

129-
if QUALIFIED_NAME_RE.match(link) and link not in xref_ignore:
138+
if QUALIFIED_NAME_RE.match(link) and link not in ignore_set:
130139
if link != title:
131140
return ':obj:`%s <%s>`' % (title, link)
132141
if wrap_unknown:
@@ -147,9 +156,7 @@ def _split_and_apply_re(s, pattern):
147156
if pattern.match(tok):
148157
results.append(tok)
149158
else:
150-
res = make_xref(
151-
tok, xref_aliases, xref_ignore, wrap_unknown
152-
)
159+
res = make_xref(tok, xref_aliases, xref_ignore)
153160
# Opening brackets immediately after a role is
154161
# bad markup. Detect that and add backslash.
155162
# :role:`type`( to :role:`type`\(

0 commit comments

Comments
 (0)