Skip to content
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

use the obj role for all See Also items #8051

Merged
merged 5 commits into from Oct 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
68 changes: 43 additions & 25 deletions sphinx/ext/napoleon/docstring.py
Expand Up @@ -1151,6 +1151,41 @@ def push_item(name: str, rest: List[str]) -> None:
items.append((name, list(rest), role))
del rest[:]

def search_inventory(inventory, name, hint=None):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is no longer needed now.

roles = list(inventory.keys())
if hint is not None:
preferred = [
role
for role in roles
if role.split(":", 1)[-1].startswith(hint)
]
roles = preferred + [role for role in roles if role not in preferred]

for role in roles:
objects = inventory[role]
found = objects.get(name, None)
if found is not None:
domain, role = role.split(":", 1)
return role

return None

def translate(func, description, role):
translations = self._config.napoleon_type_aliases
if role is not None or not translations:
return func, description, role

translated = translations.get(func, func)
match = self._name_rgx.match(translated)
if not match:
return translated, description, role

groups = match.groupdict()
role = groups["role"]
new_func = groups["name"] or groups["name2"]

return new_func, description, role

current_func = None
rest = [] # type: List[str]

Expand Down Expand Up @@ -1181,37 +1216,20 @@ def push_item(name: str, rest: List[str]) -> None:
if not items:
return []

roles = {
'method': 'meth',
'meth': 'meth',
'function': 'func',
'func': 'func',
'class': 'class',
'exception': 'exc',
'exc': 'exc',
'object': 'obj',
'obj': 'obj',
'module': 'mod',
'mod': 'mod',
'data': 'data',
'constant': 'const',
'const': 'const',
'attribute': 'attr',
'attr': 'attr'
}
if self._what is None:
func_role = 'obj'
else:
func_role = roles.get(self._what, '')
# apply type aliases
items = [
translate(func, description, role)
for func, description, role in items
]

func_role = 'obj'
lines = [] # type: List[str]
last_had_desc = True
for func, desc, role in items:
if role:
link = ':%s:`%s`' % (role, func)
elif func_role:
link = ':%s:`%s`' % (func_role, func)
else:
link = "`%s`_" % func
link = ':%s:`%s`' % (func_role, func)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems func_role would not be changed. How about embedding it into the format-string?

if desc or last_had_desc:
lines += ['']
lines += [link]
Expand Down
33 changes: 31 additions & 2 deletions tests/test_ext_napoleon_docstring.py
Expand Up @@ -1372,9 +1372,38 @@ def test_see_also_refs(self):

.. seealso::

:meth:`some`, :meth:`other`, :meth:`funcs`
:obj:`some`, :obj:`other`, :obj:`funcs`
\n\
:obj:`otherfunc`
relationship
"""
self.assertEqual(expected, actual)

docstring = """\
numpy.multivariate_normal(mean, cov, shape=None, spam=None)

See Also
--------
some, other, :func:`funcs`
otherfunc : relationship

"""
translations = {
"other": "MyClass.other",
"otherfunc": ":func:`~my_package.otherfunc`",
}
config = Config(napoleon_type_aliases=translations)
app = mock.Mock()
actual = str(NumpyDocstring(docstring, config, app, "method"))

expected = """\
numpy.multivariate_normal(mean, cov, shape=None, spam=None)

.. seealso::

:obj:`some`, :obj:`MyClass.other`, :func:`funcs`
\n\
:meth:`otherfunc`
:func:`~my_package.otherfunc`
relationship
"""
self.assertEqual(expected, actual)
Expand Down