Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue-261 Fixed missing parts of argument descriptions #262

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions fire/docstrings.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,19 +295,20 @@ def _is_arg_name(name):
"""Returns whether name is a valid arg name.

This is used to prevent multiple words (plaintext) from being misinterpreted
as an argument name. So if ":" appears in the middle of a line in a docstring,
we don't accidentally interpret the first half of that line as a single arg
name.
as an argument name. Any line that doesn't match the pattern for a valid
argument is treated as not being an argument.

Args:
name: The name of the potential arg.
Returns:
True if name looks like an arg name, False otherwise.
"""
name = name.strip()
return (name
and ' ' not in name
and ':' not in name)
# arg_pattern is a letter or underscore followed by
# zero or more letters, numbers, or underscores.
arg_pattern = r'^[a-zA-Z_]\w*$'
re.match(arg_pattern, name)
return re.match(arg_pattern, name) is not None


def _as_arg_name_and_type(text):
Expand Down Expand Up @@ -390,6 +391,7 @@ def _consume_google_args_line(line_info, state):
arg = _get_or_create_arg_by_name(state, arg_name)
arg.type.lines.append(type_str)
arg.description.lines.append(second.strip())
state.current_arg = arg
else:
if state.current_arg:
state.current_arg.description.lines.append(split_line[0])
Expand Down
55 changes: 55 additions & 0 deletions fire/docstrings_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,32 @@ def test_google_format_typed_args_and_returns(self):
)
self.assertEqual(expected_docstring_info, docstring_info)

def test_google_format_multiline_arg_description(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 has a lot of text, enough to
cover two lines.
"""
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 has a lot of text, '
'enough to cover two lines.'),
],
)
self.assertEqual(expected_docstring_info, docstring_info)

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

Expand Down Expand Up @@ -205,6 +231,35 @@ def test_numpy_format_typed_args_and_returns(self):
)
self.assertEqual(expected_docstring_info, docstring_info)

def test_numpy_format_multiline_arg_description(self):
docstring = """Docstring summary.

This is a longer description of the docstring. It spans across multiple
lines.

Parameters
----------
param1 : int
The first parameter.
param2 : str
The second parameter. This has a lot of text, enough to cover two
lines.
"""
docstring_info = docstrings.parse(docstring)
expected_docstring_info = DocstringInfo(
summary='Docstring summary.',
description='This is a longer description of the docstring. It spans '
'across multiple\nlines.',
args=[
ArgInfo(name='param1', type='int',
description='The first parameter.'),
ArgInfo(name='param2', type='str',
description='The second parameter. This has a lot of text, '
'enough to cover two lines.'),
],
)
self.assertEqual(expected_docstring_info, docstring_info)

def test_multisection_docstring(self):
docstring = """Docstring summary.

Expand Down