Skip to content

Commit

Permalink
fix: definitions corrupted with in-line comment (#99)
Browse files Browse the repository at this point in the history
* fix: retain newline when no docstring exists

* test: add tests for no docstring

* fix: also remove blank line when there is an in-line comment
  • Loading branch information
weibullguy authored Aug 7, 2022
1 parent ae24186 commit b711f12
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
9 changes: 8 additions & 1 deletion docformatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,17 @@ def _format_code(
previous_token_string = token_string
previous_token_type = token_type

# If the current token is a newline, the previous token was a
# newline or a comment, and these two sequential newlines follow a
# function definition, ignore the blank line.
if (
len(modified_tokens) > 2
and token_type in {tokenize.NL, tokenize.NEWLINE}
and modified_tokens[-2][1] == ":"
and modified_tokens[-1][0] in {tokenize.NL, tokenize.NEWLINE}
and (
modified_tokens[-2][1] == ":"
or modified_tokens[-2][0] == tokenize.COMMENT
)
and modified_tokens[-2][4][:3] == "def"
):
pass
Expand Down
43 changes: 42 additions & 1 deletion test_docformatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@ def foo():\r
''',
docformatter.format_code(input))

def test__format_code_additional_empty_line_before_doc(self):
def test_format_code_additional_empty_line_before_doc(self):
args = {'summary_wrap_length': 79,
'description_wrap_length': 72,
'pre_summary_newline': False,
Expand All @@ -632,6 +632,47 @@ def test__format_code_additional_empty_line_before_doc(self):
self.assertEqual('\n\n\ndef my_func():\n"""Summary of my function."""\npass',
docformatter._format_code('\n\n\ndef my_func():\n\n"""Summary of my function."""\npass', args))

def test_format_code_extra_newline_following_comment(self):
args = {'summary_wrap_length': 79,
'description_wrap_length': 72,
'pre_summary_newline': False,
'pre_summary_space': False,
'make_summary_multi_line': False,
'post_description_blank': False,
'force_wrap': False,
'line_range': None}

docstring = ('''\
def crash_rocket(location): # pragma: no cover
"""This is a docstring following an in-line comment."""
return location''')
self.assertEqual('''\
def crash_rocket(location): # pragma: no cover
"""This is a docstring following an in-line comment."""
return location''',
docformatter._format_code(docstring))

def test_format_code_no_docstring(self):
args = {'summary_wrap_length': 79,
'description_wrap_length': 72,
'pre_summary_newline': False,
'pre_summary_space': False,
'make_summary_multi_line': False,
'post_description_blank': False,
'force_wrap': False,
'line_range': None}
docstring = ("def pytest_addoption(parser: pytest.Parser) -> "
"None:\n register_toggle.pytest_addoption(parser)\n")
self.assertEqual(docstring,
docformatter._format_code(docstring, args))

docstring = ("def pytest_addoption(parser: pytest.Parser) -> "
"None: # pragma: no cover\n "
"register_toggle.pytest_addoption(parser)\n")
self.assertEqual(docstring,
docformatter._format_code(docstring, args))

def test_exclude(self):
sources = {"/root"}
patch_data = [
Expand Down

0 comments on commit b711f12

Please sign in to comment.