Skip to content

Commit 77cb495

Browse files
committed
FIX: Opt-in, test, uniform styling, css
1 parent fe28261 commit 77cb495

File tree

6 files changed

+39
-29
lines changed

6 files changed

+39
-29
lines changed

doc/install.rst

+7-3
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,13 @@ numpydoc_xref_param_type : bool
4646
Whether to create cross-references for the parameter types in the
4747
``Parameters``, ``Other Parameters``, ``Returns`` and ``Yields``
4848
sections of the docstring.
49-
``True`` by default.
49+
``False`` by default.
50+
51+
.. note:: Depending on the link types, the CSS styles might be different.
52+
consider overridding e.g. ``span.classifier a span.xref`` and
53+
``span.classifier a code.docutils.literal.notranslate``
54+
CSS classes to achieve a uniform appearance.
55+
5056
numpydoc_xref_aliases : dict
5157
Mappings to fully qualified paths (or correct ReST references) for the
5258
aliases/shortcuts used when specifying the types of parameters.
@@ -79,7 +85,6 @@ numpydoc_xref_aliases : dict
7985

8086
This option depends on the ``numpydoc_xref_param_type`` option
8187
being ``True``.
82-
8388
numpydoc_xref_ignore : set
8489
Words not to cross-reference. Most likely, these are common words
8590
used in parameter type descriptions that may be confused for
@@ -88,7 +93,6 @@ numpydoc_xref_ignore : set
8893
numpydoc_xref_ignore = {'type', 'optional', 'default'}
8994

9095
The default is an empty set.
91-
9296
numpydoc_edit_link : bool
9397
.. deprecated:: edit your HTML template instead
9498

numpydoc/docscrape_sphinx.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,15 @@ def _str_returns(self, name='Returns'):
8282
out += self._str_field_list(name)
8383
out += ['']
8484
for param in self[name]:
85-
if param.type:
85+
param_type = param.type
86+
if param_type:
8687
if self.xref_param_type:
87-
param.type = make_xref_param_type(
88-
param.type,
88+
param_type = make_xref_param_type(
89+
param_type,
8990
self.xref_aliases,
9091
self.xref_ignore)
91-
out += self._str_indent([typed_fmt % (param.strip(),
92-
param.type)])
92+
out += self._str_indent([typed_fmt % (param.name.strip(),
93+
param_type)])
9394
else:
9495
out += self._str_indent([untyped_fmt % param.name.strip()])
9596
if not param.desc:
@@ -220,13 +221,14 @@ def _str_param_list(self, name, fake_autosummary=False):
220221
fake_autosummary)
221222

222223
if param.type:
224+
param_type = param.type
223225
if self.xref_param_type:
224226
param_type = make_xref_param_type(
225227
param_type,
226228
self.xref_aliases,
227229
self.xref_ignore)
228230
out += self._str_indent(['%s : %s' % (display_param,
229-
param.type)])
231+
param_type)])
230232
else:
231233
out += self._str_indent([display_param])
232234
if desc and self.use_blockquotes:

numpydoc/numpydoc.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ def setup(app, get_doc_object_=get_doc_object):
210210
app.add_config_value('numpydoc_show_inherited_class_members', True, True)
211211
app.add_config_value('numpydoc_class_members_toctree', True, True)
212212
app.add_config_value('numpydoc_citation_re', '[a-z0-9_.-]+', True)
213-
app.add_config_value('numpydoc_xref_param_type', True, True)
213+
app.add_config_value('numpydoc_xref_param_type', False, True)
214214
app.add_config_value('numpydoc_xref_aliases', dict(), True)
215215
app.add_config_value('numpydoc_xref_ignore', set(), True)
216216

numpydoc/tests/test_docscrape.py

+9-8
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,8 @@ def line_by_line_compare(a, b):
335335
b = textwrap.dedent(b)
336336
a = [l.rstrip() for l in _strip_blank_lines(a).split('\n')]
337337
b = [l.rstrip() for l in _strip_blank_lines(b).split('\n')]
338-
assert all(x == y for x, y in zip(a, b))
338+
for ii, (aa, bb) in enumerate(zip(a, b)):
339+
assert aa == bb
339340

340341

341342
def test_str():
@@ -1295,33 +1296,33 @@ def test_args_and_kwargs():
12951296
"""
12961297

12971298

1298-
xref_doc_txt_expected = """
1299+
xref_doc_txt_expected = r"""
12991300
Test xref in Parameters, Other Parameters and Returns
13001301
13011302
13021303
:Parameters:
13031304
1304-
p1 : :xref_param_type:`int`
1305+
**p1** : :xref_param_type:`int`
13051306
Integer value
13061307
1307-
p2 : :xref_param_type:`float`, optional
1308+
**p2** : :xref_param_type:`float`, optional
13081309
Integer value
13091310
13101311
:Returns:
13111312
1312-
out : :xref_param_type:`array <numpy.ndarray>`
1313+
**out** : :xref_param_type:`array <numpy.ndarray>`
13131314
Numerical return value
13141315
13151316
13161317
:Other Parameters:
13171318
1318-
p3 : :xref_param_type:`list`\[:xref_param_type:`int`]
1319+
**p3** : :xref_param_type:`list`\[:xref_param_type:`int`]
13191320
List of integers
13201321
1321-
p4 : :class:`pandas.DataFrame`
1322+
**p4** : :class:`pandas.DataFrame`
13221323
A dataframe
13231324
1324-
p5 : :term:`python:sequence` of :xref_param_type:`int`
1325+
**p5** : :term:`python:sequence` of :xref_param_type:`int`
13251326
A sequence
13261327
"""
13271328

numpydoc/tests/test_xref.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# -*- encoding:utf-8 -*-
22
from __future__ import division, absolute_import, print_function
33

4-
from nose.tools import assert_equal
54
from numpydoc.xref import make_xref_param_type
65

76
xref_aliases = {
@@ -19,7 +18,7 @@
1918
}
2019

2120
# Comes mainly from numpy
22-
data = """
21+
data = r"""
2322
(...) array_like, float, optional
2423
(...) :term:`numpy:array_like`, :xref_param_type:`float`, optional
2524
@@ -125,4 +124,4 @@ def test_make_xref_param_type():
125124
xref_aliases,
126125
xref_ignore
127126
)
128-
assert_equal(result, expected_result)
127+
assert result == expected_result

numpydoc/xref.py

+12-8
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@
6666

6767

6868
def make_xref_param_type(param_type, xref_aliases, xref_ignore):
69-
"""
70-
Enclose str in a role that creates a cross-reference
69+
"""Enclose str in a role that creates a cross-reference.
70+
7171
The role ``xref_param_type`` *may be* added to any token
7272
that looks like type information and no other. The
7373
function tries to be clever and catch type information
@@ -165,21 +165,25 @@ def xref_param_type_role(role, rawtext, text, lineno, inliner,
165165
Add a pending_xref for the param_type of a field list
166166
"""
167167
has_title, title, target = split_explicit_title(text)
168+
env = inliner.document.settings.env
168169
if has_title:
169170
target = target.lstrip('~')
170171
else:
171172
if target.startswith(('~', '.')):
172173
prefix, target = target[0], target[1:]
173174
if prefix == '.':
174-
env = inliner.document.settings.env
175175
modname = env.ref_context.get('py:module')
176176
target = target[1:]
177177
target = '%s.%s' % (modname, target)
178178
elif prefix == '~':
179179
title = target.split('.')[-1]
180180

181-
contnode = nodes.Text(title, title)
182-
node = addnodes.pending_xref('', refdomain='py', refexplicit=False,
183-
reftype='class', reftarget=target)
184-
node += contnode
185-
return [node], []
181+
domain = 'py'
182+
contnode = nodes.literal(title, title)
183+
refnode = addnodes.pending_xref('', refdomain=domain, refexplicit=False,
184+
reftype='class', reftarget=target)
185+
refnode += contnode
186+
# attach information about the current scope
187+
if env:
188+
env.get_domain(domain).process_field_xref(refnode)
189+
return [refnode], []

0 commit comments

Comments
 (0)