Skip to content

WIP: Add parameter class links #57

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
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions numpydoc/docscrape_sphinx.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def _str_returns(self, name='Returns'):
out += ['']
for param, param_type, desc in self[name]:
if param_type:
param_type = make_links(param_type)
out += self._str_indent(['**%s** : %s' % (param.strip(),
param_type)])
else:
Expand All @@ -75,6 +76,7 @@ def _str_param_list(self, name):
out += ['']
for param, param_type, desc in self[name]:
if param_type:
param_type = make_links(param_type)
out += self._str_indent(['**%s** : %s' % (param.strip(),
param_type)])
else:
Expand Down Expand Up @@ -244,6 +246,25 @@ def __str__(self, indent=0, func_role="obj"):
return '\n'.join(out)


def make_links(param_type):
"""Try to parse the param type to make Sphinx links"""
constants = ('None', 'True', 'False')
# This is not a great way to parse the list yet... at the very least:
# 1. It doesn't handle e.g. {}-style entries
# 2. Using `str.replace()` instead of something smarter to find
# instances is not safe
for type_ in param_type.split():
type_ = type_.strip(',')
if type_ not in ('|', 'instance', 'of', 'or', 'optional') + constants:
param_type = param_type.replace(
type_, ':class:`%s`' % type_)
if type_ in constants:
# This doesn't link properly yet
param_type.replace(
type_, ':const:`%s`' % type_)
return param_type


class SphinxFunctionDoc(SphinxDocString, FunctionDoc):
def __init__(self, obj, doc=None, config={}):
self.load_config(config)
Expand Down