From b23148ba2aa83d97db57426d7fe27496e9c6d8e9 Mon Sep 17 00:00:00 2001 From: Eric Larson Date: Wed, 29 Jun 2016 16:17:37 -0400 Subject: [PATCH] WIP: Add parameter class links --- numpydoc/docscrape_sphinx.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/numpydoc/docscrape_sphinx.py b/numpydoc/docscrape_sphinx.py index 1eaa6a9b..17b9b14b 100644 --- a/numpydoc/docscrape_sphinx.py +++ b/numpydoc/docscrape_sphinx.py @@ -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: @@ -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: @@ -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)