Skip to content

Commit

Permalink
search the role for See Also items in the intersphinx inventory
Browse files Browse the repository at this point in the history
  • Loading branch information
keewis committed Aug 4, 2020
1 parent a721631 commit d9fb3bc
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions sphinx/ext/napoleon/docstring.py
Original file line number Diff line number Diff line change
Expand Up @@ -1131,6 +1131,7 @@ def _parse_numpydoc_see_also_section(self, content: List[str]) -> List[str]:
func_name1, func_name2, :meth:`func_name`, func_name3
"""
inventory = getattr(self._app.builder.env, "intersphinx_inventory", {})
items = []

def parse_item_name(text: str) -> Tuple[str, str]:
Expand All @@ -1151,6 +1152,25 @@ def push_item(name: str, rest: List[str]) -> None:
items.append((name, list(rest), role))
del rest[:]

def search_inventory(inventory, name, hint=None):
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

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

Expand Down Expand Up @@ -1206,6 +1226,10 @@ def push_item(name: str, rest: List[str]) -> None:
lines = [] # type: List[str]
last_had_desc = True
for func, desc, role in items:
if not role:
raw_role = search_inventory(inventory, func, hint=func_role)
role = roles.get(raw_role, raw_role)

if role:
link = ':%s:`%s`' % (role, func)
elif func_role:
Expand Down

0 comments on commit d9fb3bc

Please sign in to comment.