Skip to content

Commit

Permalink
Extend fix for issue google#309
Browse files Browse the repository at this point in the history
  • Loading branch information
xosnos committed Apr 24, 2021
1 parent 9496fc7 commit 6ad659e
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 5 deletions.
11 changes: 7 additions & 4 deletions fire/docstrings.py
Expand Up @@ -339,7 +339,6 @@ def _as_arg_name_and_type(text):
if len(tokens) < 2:
return None
if _is_arg_name(tokens[0]) and not _is_arg_name(tokens[1]):
# if _is_arg_name(tokens[0]):
type_token = ' '.join(tokens[1:])
type_token = type_token.lstrip('{([').rstrip('])}')
return tokens[0], type_token
Expand Down Expand Up @@ -393,13 +392,18 @@ def _consume_google_args_line(line_info, state):
split_line = line_info.remaining.split(':', 1)
if len(split_line) > 1:
first, second = split_line # first is either the "arg" or "arg (type)"
if _is_arg_name(first.strip()):
new_line = line_info.indentation <= line_info.previous.indentation
if _is_arg_name(first.strip()) and (
state.current_arg is None or new_line
):
arg = _get_or_create_arg_by_name(state, first.strip())
arg.description.lines.append(second.strip())
state.current_arg = arg
else:
arg_name_and_type = _as_arg_name_and_type(first)
if arg_name_and_type:
if arg_name_and_type and (
state.current_arg is None or new_line
):
arg_name, type_str = arg_name_and_type
arg = _get_or_create_arg_by_name(state, arg_name)
arg.type.lines.append(type_str)
Expand All @@ -408,7 +412,6 @@ def _consume_google_args_line(line_info, state):
else:
if state.current_arg:
state.current_arg.description.lines.append(first + ':' + second)
# state.current_arg.description.lines.append(split_line[0])
else:
if state.current_arg:
state.current_arg.description.lines.append(split_line[0])
Expand Down
56 changes: 55 additions & 1 deletion fire/docstrings_test.py
Expand Up @@ -170,7 +170,7 @@ def test_google_format_multiline_arg_description(self):
)
self.assertEqual(expected_docstring_info, docstring_info)

def test_google_format_multiline_arg_description_with_colon(self):
def test_google_format_multiline_arg_description_colon(self):
docstring = """Docstring summary.
This is a longer description of the docstring. It spans multiple lines, as
Expand All @@ -197,6 +197,60 @@ def test_google_format_multiline_arg_description_with_colon(self):
)
self.assertEqual(expected_docstring_info, docstring_info)

def test_google_format_multiline_arg_description_colon_wrapped(self):
docstring = """Docstring summary.
This is a longer description of the docstring. It spans multiple lines, as
is allowed.
Args:
param1 (int): The first parameter.
param2 (str): The second parameter. This description contains a
colon : after the first word of the wrapped line.
"""
docstring_info = docstrings.parse(docstring)
expected_docstring_info = DocstringInfo(
summary='Docstring summary.',
description='This is a longer description of the docstring. It spans '
'multiple lines, as\nis allowed.',
args=[
ArgInfo(name='param1', type='int',
description='The first parameter.'),
ArgInfo(name='param2', type='str',
description='The second parameter. This description '
'contains a colon : after the first word '
'of the wrapped line.'),
],
)
self.assertEqual(expected_docstring_info, docstring_info)

def test_google_format_multiline_arg_description_colon_parenthesis(self):
docstring = """Docstring summary.
This is a longer description of the docstring. It spans multiple lines, as
is allowed.
Args:
param1 (int): The first parameter.
param2 (str): The second parameter. This description contains a
colon (and): parenthesis after the first word of the wrapped line.
"""
docstring_info = docstrings.parse(docstring)
expected_docstring_info = DocstringInfo(
summary='Docstring summary.',
description='This is a longer description of the docstring. It spans '
'multiple lines, as\nis allowed.',
args=[
ArgInfo(name='param1', type='int',
description='The first parameter.'),
ArgInfo(name='param2', type='str',
description='The second parameter. This description '
'contains a colon (and): parenthesis after '
'the first word of the wrapped line.'),
],
)
self.assertEqual(expected_docstring_info, docstring_info)

def test_rst_format_typed_args_and_returns(self):
docstring = """Docstring summary.
Expand Down

0 comments on commit 6ad659e

Please sign in to comment.