-
-
Notifications
You must be signed in to change notification settings - Fork 166
Add cross-reference links to parameter types #150
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
616c4b9
Add cross-reference links to parameter types
has2k1 0f5c33c
Changes after review
has2k1 f926c46
Fix issues with generated markup for param types
has2k1 a6ddef6
Use nodes.Text instead of nodes.inline
has2k1 aaa48d7
Split on comma-space, care with brackets & quotes
has2k1 1843f5e
Make the ignore set for xrefs an option
has2k1 4dd6657
DOC: No spaces allowed in xref_aliases keys
has2k1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,6 +42,53 @@ numpydoc_use_blockqutoes : bool | |
Until version 0.8, parameter definitions were shown as blockquotes, rather | ||
than in a definition list. If your styling requires blockquotes, switch | ||
this config option to True. This option will be removed in version 0.10. | ||
numpydoc_xref_param_type : bool | ||
Whether to create cross-references for the parameter types in the | ||
``Parameters``, ``Other Parameters``, ``Returns`` and ``Yields`` | ||
sections of the docstring. | ||
``True`` by default. | ||
numpydoc_xref_aliases : dict | ||
Mappings to fully qualified paths (or correct ReST references) for the | ||
aliases/shortcuts used when specifying the types of parameters. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mention no spaces allowed |
||
The keys should not have any spaces. Together with the ``intersphinx`` | ||
extension, you can map to links in any documentation. | ||
The default is an empty ``dict``. | ||
|
||
If you have the following ``intersphinx`` namespace configuration:: | ||
|
||
intersphinx_mapping = { | ||
'python': ('https://docs.python.org/3/', None), | ||
'numpy': ('https://docs.scipy.org/doc/numpy', None), | ||
} | ||
|
||
A useful ``dict`` may look like the following:: | ||
|
||
numpydoc_xref_aliases = { | ||
# python | ||
'sequence': ':term:`python:sequence`', | ||
'iterable': ':term:`python:iterable`', | ||
'string': 'str', | ||
# numpy | ||
'array': 'numpy.ndarray', | ||
'dtype': 'numpy.dtype', | ||
'ndarray': 'numpy.ndarray', | ||
'matrix': 'numpy.matrix', | ||
'array-like': ':term:`numpy:array_like`', | ||
'array_like': ':term:`numpy:array_like`', | ||
} | ||
|
||
This option depends on the ``numpydoc_xref_param_type`` option | ||
being ``True``. | ||
|
||
numpydoc_xref_ignore : set | ||
Words not to cross-reference. Most likely, these are common words | ||
used in parameter type descriptions that may be confused for | ||
classes of the same name. For example:: | ||
|
||
numpydoc_xref_ignore = {'type', 'optional', 'default'} | ||
|
||
The default is an empty set. | ||
|
||
numpydoc_edit_link : bool | ||
.. deprecated:: edit your HTML template instead | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,7 @@ | |
raise RuntimeError("Sphinx 1.0.1 or newer is required") | ||
|
||
from .docscrape_sphinx import get_doc_object, SphinxDocString | ||
from .xref import xref_param_type_role | ||
from . import __version__ | ||
|
||
if sys.version_info[0] >= 3: | ||
|
@@ -76,7 +77,11 @@ def mangle_docstrings(app, what, name, obj, options, lines): | |
'show_class_members': app.config.numpydoc_show_class_members, | ||
'show_inherited_class_members': | ||
app.config.numpydoc_show_inherited_class_members, | ||
'class_members_toctree': app.config.numpydoc_class_members_toctree} | ||
'class_members_toctree': app.config.numpydoc_class_members_toctree, | ||
'xref_param_type': app.config.numpydoc_xref_param_type, | ||
'xref_aliases': app.config.numpydoc_xref_aliases, | ||
'xref_ignore': app.config.numpydoc_xref_ignore, | ||
} | ||
|
||
u_NL = sixu('\n') | ||
if what == 'module': | ||
|
@@ -137,6 +142,7 @@ def setup(app, get_doc_object_=get_doc_object): | |
global get_doc_object | ||
get_doc_object = get_doc_object_ | ||
|
||
app.add_role('xref_param_type', xref_param_type_role) | ||
app.connect('autodoc-process-docstring', mangle_docstrings) | ||
app.connect('autodoc-process-signature', mangle_signature) | ||
app.add_config_value('numpydoc_edit_link', None, False) | ||
|
@@ -146,6 +152,9 @@ def setup(app, get_doc_object_=get_doc_object): | |
app.add_config_value('numpydoc_show_inherited_class_members', True, True) | ||
app.add_config_value('numpydoc_class_members_toctree', True, True) | ||
app.add_config_value('numpydoc_citation_re', '[a-z0-9_.-]+', True) | ||
app.add_config_value('numpydoc_xref_param_type', True, True) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like the default is still True, so it's opt-out currently. |
||
app.add_config_value('numpydoc_xref_aliases', dict(), True) | ||
app.add_config_value('numpydoc_xref_ignore', set(), True) | ||
|
||
# Extra mangling domains | ||
app.add_domain(NumpyPythonDomain) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
# -*- encoding:utf-8 -*- | ||
from __future__ import division, absolute_import, print_function | ||
|
||
from nose.tools import assert_equal | ||
from numpydoc.xref import make_xref_param_type | ||
|
||
xref_aliases = { | ||
# python | ||
'sequence': ':term:`python:sequence`', | ||
'iterable': ':term:`python:iterable`', | ||
'string': 'str', | ||
# numpy | ||
'array': 'numpy.ndarray', | ||
'dtype': 'numpy.dtype', | ||
'ndarray': 'numpy.ndarray', | ||
'matrix': 'numpy.matrix', | ||
'array-like': ':term:`numpy:array_like`', | ||
'array_like': ':term:`numpy:array_like`', | ||
} | ||
|
||
# Comes mainly from numpy | ||
data = """ | ||
(...) array_like, float, optional | ||
(...) :term:`numpy:array_like`, :xref_param_type:`float`, optional | ||
|
||
(2,) ndarray | ||
(2,) :xref_param_type:`ndarray <numpy.ndarray>` | ||
|
||
(...,M,N) array_like | ||
(...,M,N) :term:`numpy:array_like` | ||
|
||
(..., M, N) array_like | ||
(..., :xref_param_type:`M`, :xref_param_type:`N`) :term:`numpy:array_like` | ||
|
||
(float, float), optional | ||
(:xref_param_type:`float`, :xref_param_type:`float`), optional | ||
|
||
1-D array or sequence | ||
1-D :xref_param_type:`array <numpy.ndarray>` or :term:`python:sequence` | ||
|
||
array of str or unicode-like | ||
:xref_param_type:`array <numpy.ndarray>` of :xref_param_type:`str` or unicode-like | ||
|
||
array_like of float | ||
:term:`numpy:array_like` of :xref_param_type:`float` | ||
|
||
bool or callable | ||
:xref_param_type:`bool` or :xref_param_type:`callable` | ||
|
||
int in [0, 255] | ||
:xref_param_type:`int` in [0, 255] | ||
|
||
int or None, optional | ||
:xref_param_type:`int` or :xref_param_type:`None`, optional | ||
|
||
list of str or array_like | ||
:xref_param_type:`list` of :xref_param_type:`str` or :term:`numpy:array_like` | ||
|
||
sequence of array_like | ||
:term:`python:sequence` of :term:`numpy:array_like` | ||
|
||
str or pathlib.Path | ||
:xref_param_type:`str` or :xref_param_type:`pathlib.Path` | ||
|
||
{'', string}, optional | ||
{'', :xref_param_type:`string <str>`}, optional | ||
|
||
{'C', 'F', 'A', or 'K'}, optional | ||
{'C', 'F', 'A', or 'K'}, optional | ||
|
||
{'linear', 'lower', 'higher', 'midpoint', 'nearest'} | ||
{'linear', 'lower', 'higher', 'midpoint', 'nearest'} | ||
|
||
{False, True, 'greedy', 'optimal'} | ||
{:xref_param_type:`False`, :xref_param_type:`True`, 'greedy', 'optimal'} | ||
|
||
{{'begin', 1}, {'end', 0}}, {string, int} | ||
{{'begin', 1}, {'end', 0}}, {:xref_param_type:`string <str>`, :xref_param_type:`int`} | ||
|
||
callable f'(x,*args) | ||
:xref_param_type:`callable` f'(x,*args) | ||
|
||
callable ``fhess(x, *args)``, optional | ||
:xref_param_type:`callable` ``fhess(x, *args)``, optional | ||
|
||
spmatrix (format: ``csr``, ``bsr``, ``dia`` or coo``) | ||
:xref_param_type:`spmatrix` (format: ``csr``, ``bsr``, ``dia`` or coo``) | ||
|
||
:ref:`strftime <strftime-strptime-behavior>` | ||
:ref:`strftime <strftime-strptime-behavior>` | ||
|
||
callable or :ref:`strftime <strftime-strptime-behavior>` | ||
:xref_param_type:`callable` or :ref:`strftime <strftime-strptime-behavior>` | ||
|
||
callable or :ref:`strftime behavior <strftime-strptime-behavior>` | ||
:xref_param_type:`callable` or :ref:`strftime behavior <strftime-strptime-behavior>` | ||
|
||
list(int) | ||
:xref_param_type:`list`\(:xref_param_type:`int`) | ||
|
||
list[int] | ||
:xref_param_type:`list`\[:xref_param_type:`int`] | ||
|
||
dict(str, int) | ||
:xref_param_type:`dict`\(:xref_param_type:`str`, :xref_param_type:`int`) | ||
|
||
dict[str, int] | ||
:xref_param_type:`dict`\[:xref_param_type:`str`, :xref_param_type:`int`] | ||
|
||
tuple(float, float) | ||
:xref_param_type:`tuple`\(:xref_param_type:`float`, :xref_param_type:`float`) | ||
|
||
dict[tuple(str, str), int] | ||
:xref_param_type:`dict`\[:xref_param_type:`tuple`\(:xref_param_type:`str`, :xref_param_type:`str`), :xref_param_type:`int`] | ||
""" # noqa: E501 | ||
|
||
xref_ignore = {'or', 'in', 'of', 'default', 'optional'} | ||
|
||
|
||
def test_make_xref_param_type(): | ||
for s in data.strip().split('\n\n'): | ||
param_type, expected_result = s.split('\n') | ||
result = make_xref_param_type( | ||
param_type, | ||
xref_aliases, | ||
xref_ignore | ||
) | ||
assert_equal(result, expected_result) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it's opt-in, then this docstring should be changed to say "
False
by default"