Closes #1831: [Napoleon] Makes google type regex greedy to consume entire type, even if it contains colons

This commit is contained in:
Rob Ruana 2015-05-03 16:28:39 -07:00
parent 80eb821ec8
commit 42f2b30579
2 changed files with 59 additions and 12 deletions

View File

@ -23,8 +23,8 @@ from sphinx.util.pycompat import UnicodeMixin
_directive_regex = re.compile(r'\.\. \S+::')
_google_untyped_arg_regex = re.compile(r'\s*(.+?)\s*:\s*(.*)')
_google_typed_arg_regex = re.compile(r'\s*(.+?)\s*\(\s*(.+?)\s*\)\s*:\s*(.*)')
_google_untyped_arg_regex = re.compile(r'(.+)\s*(?<!:):(?!:)\s*(.*)')
_google_typed_arg_regex = re.compile(r'(.+)\((.+)\)\s*(?<!:):(?!:)\s*(.*)')
class GoogleDocstring(UnicodeMixin):
@ -207,15 +207,15 @@ class GoogleDocstring(UnicodeMixin):
if parse_type:
match = _google_typed_arg_regex.match(line)
if match:
_name = match.group(1)
_type = match.group(2)
_desc = match.group(3)
_name = match.group(1).strip()
_type = match.group(2).strip()
_desc = match.group(3).strip()
if not match:
match = _google_untyped_arg_regex.match(line)
if match:
_name = match.group(1)
_desc = match.group(2)
_name = match.group(1).strip()
_desc = match.group(2).strip()
if _name[:2] == '**':
_name = r'\*\*'+_name[2:]
@ -244,14 +244,14 @@ class GoogleDocstring(UnicodeMixin):
_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)
_name = match.group(1).strip()
_type = match.group(2).strip()
_desc = match.group(3).strip()
else:
match = _google_untyped_arg_regex.match(lines[0])
if match:
_type = match.group(1)
_desc = match.group(2)
_type = match.group(1).strip()
_desc = match.group(2).strip()
if match:
lines[0] = _desc
_desc = lines

View File

@ -344,6 +344,53 @@ Returns:
actual = str(GoogleDocstring(docstring))
self.assertEqual(expected, actual)
def test_colon_in_return_type(self):
docstring = """Example property.
Returns:
:py:class:`~.module.submodule.SomeClass`: an example instance
if available, None if not available.
"""
expected = """Example property.
:returns: an example instance
if available, None if not available.
:rtype: :py:class:`~.module.submodule.SomeClass`
"""
actual = str(GoogleDocstring(docstring))
self.assertEqual(expected, actual)
def test_kwargs_in_arguments(self):
docstring = """Allows to create attributes binded to this device.
Some other paragraph.
Code sample for usage::
dev.bind(loopback=Loopback)
dev.loopback.configure()
Arguments:
**kwargs: name/class pairs that will create resource-managers
bound as instance attributes to this instance. See code
example above.
"""
expected = """Allows to create attributes binded to this device.
Some other paragraph.
Code sample for usage::
dev.bind(loopback=Loopback)
dev.loopback.configure()
:param \\*\\*kwargs: name/class pairs that will create resource-managers
bound as instance attributes to this instance. See code
example above.
"""
actual = str(GoogleDocstring(docstring))
self.assertEqual(expected, actual)
class NumpyDocstringTest(BaseDocstringTest):
docstrings = [(