Skip to content

Commit 045f35c

Browse files
authored
Merge pull request #706 from s01st/patch-numpy-docs
Patch for `NumpyDocString` to add more parameter supported classes
2 parents 3a374d6 + 459f7db commit 045f35c

1 file changed

Lines changed: 32 additions & 3 deletions

File tree

fastcore/docscrape.py

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,23 +93,46 @@ def __str__(self):
9393

9494

9595
SECTIONS = 'Summary Extended Yields Receives Other Raises Warns Warnings See Also Notes References Examples Attributes Methods'.split()
96+
PARAM_SECTIONS = {"Parameters", "Other Parameters", "Attributes", "Methods", "Raises", "Warns", "Yields", "Receives"}
9697

9798
class NumpyDocString(Mapping):
9899
"Parses a numpydoc string to an abstract representation"
99100
# See the NumPy Doc Manual https://numpydoc.readthedocs.io/en/latest/format.html>
101+
100102
sections = {o:[] for o in SECTIONS}
101103
sections['Summary'] = ['']
102104
sections['Parameters'] = []
103105
sections['Returns'] = []
104-
105-
def __init__(self, docstring, config=None):
106+
param_sections: set[str] = set(PARAM_SECTIONS)
107+
108+
def __init__(self, docstring, config=None, supported_sections = SECTIONS, supports_params = PARAM_SECTIONS):
109+
110+
if supports_params is None: supports_params = set(PARAM_SECTIONS)
111+
else:
112+
missing = set(supports_params) - set(self.param_sections)
113+
for sec in missing: self.param_sections.add(sec)
114+
115+
116+
if supported_sections is None: supported_sections = set(SECTIONS)
117+
else:
118+
missing = set(supported_sections) - set(self.sections.keys())
119+
for sec in missing: self.sections[sec] = []
120+
121+
106122
docstring = textwrap.dedent(docstring).split('\n')
107123
self._doc = Reader(docstring)
108124
self._parsed_data = copy.deepcopy(self.sections)
109125
self._parse()
126+
110127
self['Parameters'] = {o.name:o for o in self['Parameters']}
111128
if self['Returns']: self['Returns'] = self['Returns'][0]
112-
for section in SECTIONS: self[section] = dedent_lines(self[section], split=False)
129+
130+
for sec in supports_params:
131+
if sec in self._parsed_data:
132+
self._parsed_data[sec] = self._normalize_param_section(self._parsed_data[sec])
133+
134+
for section in SECTIONS:
135+
self[section] = dedent_lines(self[section], split=False)
113136

114137
def __iter__(self): return iter(self._parsed_data)
115138
def __len__(self): return len(self._parsed_data)
@@ -174,6 +197,12 @@ def _parse_param_list(self, content, single_element_is_type=False):
174197
params.append(Parameter(arg_name, arg_type, desc))
175198
return params
176199

200+
def _normalize_param_section(self, val):
201+
"""Convert lists of `Parameter` objects into a dict or clean list."""
202+
if not isinstance(val, list) or not val: return val
203+
if not isinstance(val[0], Parameter): return val
204+
return {p.name: p for p in val}
205+
177206
def _parse_summary(self):
178207
"""Grab signature (if given) and summary"""
179208
if self._is_at_section(): return

0 commit comments

Comments
 (0)