Skip to content

Commit 30d8843

Browse files
authored
Merge pull request #197 from larsoner/xref-param-type
ENH: Xref param type
2 parents c2e8b8f + 4c9698e commit 30d8843

12 files changed

+506
-32
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@
88
*.swo
99
build
1010
dist
11+
doc/_build

.travis.yml

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
11
# After changing this file, check it on:
22
# http://lint.travis-ci.org/
33
language: python
4+
dist: xenial
45
sudo: false
5-
env:
6-
- SPHINX_SPEC="Sphinx==1.2.3"
7-
- SPHINX_SPEC="Sphinx"
86
matrix:
97
include:
10-
- python: 3.6
8+
- python: 3.7
9+
env: SPHINX_SPEC="==1.2.3" SPHINXOPTS=""
10+
- python: 3.7
1111
- python: 2.7
12-
env:
13-
- SPHINXOPTS='-W'
1412
cache:
1513
directories:
1614
- $HOME/.cache/pip
1715
before_install:
1816
- sudo apt-get install texlive texlive-latex-extra latexmk
1917
- pip install --upgrade pip setuptools # Upgrade pip and setuptools to get ones with `wheel` support
20-
- pip install pytest numpy matplotlib ${SPHINX_SPEC}
18+
- pip install pytest numpy matplotlib sphinx${SPHINX_SPEC}
2119
script:
2220
- |
2321
python setup.py sdist

doc/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#
33

44
# You can set these variables from the command line.
5-
SPHINXOPTS =
5+
SPHINXOPTS = -nWT --keep-going
66
SPHINXBUILD = sphinx-build
77
PAPER =
88
BUILDDIR = _build

doc/conf.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@
8282
release = numpydoc.__version__
8383
version = re.sub(r'(\d+\.\d+)\.\d+(.*)', r'\1\2', numpydoc.__version__)
8484
version = re.sub(r'(\.dev\d+).*?$', r'\1', version)
85+
numpydoc_xref_param_type = True
86+
numpydoc_xref_ignore = {'optional', 'type_without_description', 'BadException'}
8587

8688
# The language for content autogenerated by Sphinx. Refer to documentation
8789
# for a list of supported languages.
@@ -269,5 +271,6 @@
269271
# Example configuration for intersphinx: refer to the Python standard library.
270272
intersphinx_mapping = {
271273
'python': ('http://docs.python.org/', None),
272-
'scikitlearn': ('http://scikit-learn.org/stable/', None),
274+
'numpy': ('https://www.numpy.org/devdocs', None),
275+
'sklearn': ('http://scikit-learn.org/stable/', None),
273276
}

doc/example.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,10 @@ def foo(var1, var2, long_var_name='hi'):
7878
7979
See Also
8080
--------
81-
otherfunc : relationship (optional)
82-
newfunc : Relationship (optional), which could be fairly long, in which
83-
case the line wraps here.
84-
thirdfunc, fourthfunc, fifthfunc
81+
numpy.array : relationship (optional)
82+
numpy.ndarray : Relationship (optional), which could be fairly long, in
83+
which case the line wraps here.
84+
numpy.dot, numpy.linalg.norm, numpy.eye
8585
8686
Notes
8787
-----

doc/install.rst

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,52 @@ numpydoc_attributes_as_param_list : bool
4747
as the Parameter section. If it's False, the Attributes section will be
4848
formatted as the Methods section using an autosummary table.
4949
``True`` by default.
50+
numpydoc_xref_param_type : bool
51+
Whether to create cross-references for the parameter types in the
52+
``Parameters``, ``Other Parameters``, ``Returns`` and ``Yields``
53+
sections of the docstring.
54+
``False`` by default.
55+
56+
.. note:: Depending on the link types, the CSS styles might be different.
57+
consider overridding e.g. ``span.classifier a span.xref`` and
58+
``span.classifier a code.docutils.literal.notranslate``
59+
CSS classes to achieve a uniform appearance.
60+
61+
numpydoc_xref_aliases : dict
62+
Mappings to fully qualified paths (or correct ReST references) for the
63+
aliases/shortcuts used when specifying the types of parameters.
64+
The keys should not have any spaces. Together with the ``intersphinx``
65+
extension, you can map to links in any documentation.
66+
The default is an empty ``dict``.
67+
68+
If you have the following ``intersphinx`` namespace configuration::
69+
70+
intersphinx_mapping = {
71+
'python': ('https://docs.python.org/3/', None),
72+
'numpy': ('https://docs.scipy.org/doc/numpy', None),
73+
...
74+
}
75+
76+
The default ``numpydoc_xref_aliases`` will supply some common ``Python``
77+
standard library and ``NumPy`` names for you. Then for your module, a useful
78+
``dict`` may look like the following (e.g., if you were documenting
79+
:mod:`sklearn.model_selection`)::
80+
81+
numpydoc_xref_aliases = {
82+
'LeaveOneOut': 'sklearn.model_selection.LeaveOneOut',
83+
...
84+
}
85+
86+
This option depends on the ``numpydoc_xref_param_type`` option
87+
being ``True``.
88+
numpydoc_xref_ignore : set
89+
Words not to cross-reference. Most likely, these are common words
90+
used in parameter type descriptions that may be confused for
91+
classes of the same name. For example::
92+
93+
numpydoc_xref_ignore = {'type', 'optional', 'default'}
94+
95+
The default is an empty set.
5096
numpydoc_edit_link : bool
5197
.. deprecated:: edit your HTML template instead
5298

numpydoc/docscrape_sphinx.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from sphinx.jinja2glue import BuiltinTemplateLoader
1818

1919
from .docscrape import NumpyDocString, FunctionDoc, ClassDoc
20+
from .xref import make_xref
2021

2122
if sys.version_info[0] >= 3:
2223
sixu = lambda s: s
@@ -37,6 +38,9 @@ def load_config(self, config):
3738
self.use_blockquotes = config.get('use_blockquotes', False)
3839
self.class_members_toctree = config.get('class_members_toctree', True)
3940
self.attributes_as_param_list = config.get('attributes_as_param_list', True)
41+
self.xref_param_type = config.get('xref_param_type', False)
42+
self.xref_aliases = config.get('xref_aliases', dict())
43+
self.xref_ignore = config.get('xref_ignore', set())
4044
self.template = config.get('template', None)
4145
if self.template is None:
4246
template_dirs = [os.path.join(os.path.dirname(__file__), 'templates')]
@@ -79,11 +83,17 @@ def _str_returns(self, name='Returns'):
7983
out += self._str_field_list(name)
8084
out += ['']
8185
for param in self[name]:
86+
param_type = param.type
87+
if param_type and self.xref_param_type:
88+
param_type = make_xref(
89+
param_type,
90+
self.xref_aliases,
91+
self.xref_ignore)
8292
if param.name:
8393
out += self._str_indent([named_fmt % (param.name.strip(),
84-
param.type)])
94+
param_type)])
8595
else:
86-
out += self._str_indent([unnamed_fmt % param.type.strip()])
96+
out += self._str_indent([unnamed_fmt % param_type.strip()])
8797
if not param.desc:
8898
out += self._str_indent(['..'], 8)
8999
else:
@@ -158,10 +168,8 @@ def _process_param(self, param, desc, fake_autosummary):
158168

159169
prefix = getattr(self, '_name', '')
160170
if prefix:
161-
autosum_prefix = '~%s.' % prefix
162171
link_prefix = '%s.' % prefix
163172
else:
164-
autosum_prefix = ''
165173
link_prefix = ''
166174

167175
# Referenced object has a docstring
@@ -213,8 +221,15 @@ def _str_param_list(self, name, fake_autosummary=False):
213221
parts = []
214222
if display_param:
215223
parts.append(display_param)
216-
if param.type:
217-
parts.append(param.type)
224+
param_type = param.type
225+
if param_type:
226+
param_type = param.type
227+
if self.xref_param_type:
228+
param_type = make_xref(
229+
param_type,
230+
self.xref_aliases,
231+
self.xref_ignore)
232+
parts.append(param_type)
218233
out += self._str_indent([' : '.join(parts)])
219234

220235
if desc and self.use_blockquotes:

numpydoc/numpydoc.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
raise RuntimeError("Sphinx 1.0.1 or newer is required")
3838

3939
from .docscrape_sphinx import get_doc_object
40+
from .xref import DEFAULT_LINKS
4041
from . import __version__
4142

4243
if sys.version_info[0] >= 3:
@@ -154,7 +155,11 @@ def mangle_docstrings(app, what, name, obj, options, lines):
154155
app.config.numpydoc_show_inherited_class_members,
155156
'class_members_toctree': app.config.numpydoc_class_members_toctree,
156157
'attributes_as_param_list':
157-
app.config.numpydoc_attributes_as_param_list}
158+
app.config.numpydoc_attributes_as_param_list,
159+
'xref_param_type': app.config.numpydoc_xref_param_type,
160+
'xref_aliases': app.config.numpydoc_xref_aliases,
161+
'xref_ignore': app.config.numpydoc_xref_ignore,
162+
}
158163

159164
cfg.update(options or {})
160165
u_NL = sixu('\n')
@@ -218,6 +223,7 @@ def setup(app, get_doc_object_=get_doc_object):
218223

219224
app.setup_extension('sphinx.ext.autosummary')
220225

226+
app.connect('builder-inited', update_config)
221227
app.connect('autodoc-process-docstring', mangle_docstrings)
222228
app.connect('autodoc-process-signature', mangle_signature)
223229
app.connect('doctree-read', relabel_references)
@@ -230,6 +236,9 @@ def setup(app, get_doc_object_=get_doc_object):
230236
app.add_config_value('numpydoc_class_members_toctree', True, True)
231237
app.add_config_value('numpydoc_citation_re', '[a-z0-9_.-]+', True)
232238
app.add_config_value('numpydoc_attributes_as_param_list', True, True)
239+
app.add_config_value('numpydoc_xref_param_type', False, True)
240+
app.add_config_value('numpydoc_xref_aliases', dict(), True)
241+
app.add_config_value('numpydoc_xref_ignore', set(), True)
233242

234243
# Extra mangling domains
235244
app.add_domain(NumpyPythonDomain)
@@ -239,6 +248,14 @@ def setup(app, get_doc_object_=get_doc_object):
239248
'parallel_read_safe': True}
240249
return metadata
241250

251+
252+
def update_config(app):
253+
"""Update the configuration with default values."""
254+
for key, value in DEFAULT_LINKS.items():
255+
if key not in app.config.numpydoc_xref_aliases:
256+
app.config.numpydoc_xref_aliases[key] = value
257+
258+
242259
# ------------------------------------------------------------------------------
243260
# Docstring-mangling domains
244261
# ------------------------------------------------------------------------------

0 commit comments

Comments
 (0)