diff options
| author | Rob Ruana <rob@relentlessidiot.com> | 2014-03-21 19:21:18 -0400 |
|---|---|---|
| committer | Rob Ruana <rob@relentlessidiot.com> | 2014-03-21 19:21:18 -0400 |
| commit | d2e0fb667cad9f93b77de93e66b8d184514fcfca (patch) | |
| tree | d13554686286e7ec821e42fb677d1287397deff3 /sphinx | |
| parent | ae1e88428e8729940a1bf9c83c373c19172092e3 (diff) | |
| download | sphinx-d2e0fb667cad9f93b77de93e66b8d184514fcfca.tar.gz | |
Closes #1429: Adds smarter Args parsing for Google style docstrings.
Diffstat (limited to 'sphinx')
| -rw-r--r-- | sphinx/ext/napoleon/docstring.py | 45 |
1 files changed, 30 insertions, 15 deletions
diff --git a/sphinx/ext/napoleon/docstring.py b/sphinx/ext/napoleon/docstring.py index 24f4423e..1f41b6b6 100644 --- a/sphinx/ext/napoleon/docstring.py +++ b/sphinx/ext/napoleon/docstring.py @@ -24,7 +24,8 @@ if sys.version_info[0] >= 3: _directive_regex = re.compile(r'\.\. \S+::') -_field_parens_regex = re.compile(r'\s*(\w+)\s*\(\s*(.+?)\s*\)') +_google_untyped_arg_regex = re.compile(r'\s*(\w+)\s*:\s*(.*)') +_google_typed_arg_regex = re.compile(r'\s*(\w+)\s*\(\s*(.+?)\s*\)\s*:\s*(.*)') class GoogleDocstring(object): @@ -213,12 +214,22 @@ class GoogleDocstring(object): def _consume_field(self, parse_type=True, prefer_type=False): line = self._line_iter.next() - _name, _, _desc = line.partition(':') - _name, _type, _desc = _name.strip(), '', _desc.strip() - match = _field_parens_regex.match(_name) - if parse_type and match: - _name = match.group(1) - _type = match.group(2) + + match = None + _name, _type, _desc = line.strip(), '', '' + if parse_type: + match = _google_typed_arg_regex.match(line) + if match: + _name = match.group(1) + _type = match.group(2) + _desc = match.group(3) + + if not match: + match = _google_untyped_arg_regex.match(line) + if match: + _name = match.group(1) + _desc = match.group(2) + if prefer_type and not _type: _type, _name = _name, _type indent = self._get_indent(line) + 1 @@ -238,17 +249,21 @@ class GoogleDocstring(object): def _consume_returns_section(self): lines = self._dedent(self._consume_to_next_section()) if lines: - if ':' in lines[0]: - _type, _, _desc = lines[0].partition(':') - _name, _type, _desc = '', _type.strip(), _desc.strip() - match = _field_parens_regex.match(_type) + _name, _type, _desc = '', '', lines + match = _google_typed_arg_regex.match(lines[0]) + if match: + _name = match.group(1) + _type = match.group(2) + _desc = match.group(3) + else: + match = _google_untyped_arg_regex.match(lines[0]) if match: - _name = match.group(1) - _type = match.group(2) + _type = match.group(1) + _desc = match.group(2) + if match: lines[0] = _desc _desc = lines - else: - _name, _type, _desc = '', '', lines + _desc = self.__class__(_desc, self._config).lines() return [(_name, _type, _desc,)] else: |
