Skip to content

Commit

Permalink
fix: removing newline after shebang (#188)
Browse files Browse the repository at this point in the history
* fix: removing newline after shebang

* test: for removing newline after shebang
  • Loading branch information
weibullguy committed Apr 25, 2023
1 parent 1e9076a commit 1cb6545
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 16 deletions.
20 changes: 13 additions & 7 deletions src/docformatter/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -528,14 +528,20 @@ def _do_remove_blank_lines_after_definitions(modified_tokens):
"""
for _idx, _token in enumerate(modified_tokens):
if _token[0] == 3:
# Remove newline between variable definition and docstring.
j = 1
while modified_tokens[_idx - j][
4
] == "\n" and not modified_tokens[_idx - j - 1][
4
].strip().endswith(
'"""'

# Remove newline between variable definition and docstring
# unless is separating docstring from:
# * A previous docstring.
# * The file's shebang.
while (
modified_tokens[_idx - j][4] == "\n"
and not (
modified_tokens[_idx - j - 1][4]
.strip()
.endswith('"""')
)
and not modified_tokens[_idx - j - 1][4].startswith("#!/")
):
modified_tokens.pop(_idx - j)
j += 1
Expand Down
12 changes: 6 additions & 6 deletions src/docformatter/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,7 @@ def normalize_summary(summary: str) -> str:
# Remove trailing whitespace
summary = summary.rstrip()

# Add period at end of sentence and capitalize the first word of the
# summary.
# Add period at end of sentence.
if (
summary
and (summary[-1].isalnum() or summary[-1] in ['"', "'"])
Expand All @@ -114,10 +113,11 @@ def normalize_summary(summary: str) -> str:
summary += "."

with contextlib.suppress(IndexError):
# Look for underscores in the first word, this would typically
# indicate the first word is a variable name or some other
# non-standard English word.
if "_" not in summary.split(" ", 1)[0]:
# Look for underscores, periods in the first word, this would typically
# indicate the first word is a variable name, file name, or some other
# non-standard English word. If none of these exist capitalize the
# first word of the summary.
if all(char not in summary.split(" ", 1)[0] for char in ["_", "."]):
summary = summary[0].upper() + summary[1:]

return summary
Expand Down
34 changes: 31 additions & 3 deletions tests/test_format_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -853,9 +853,9 @@ def test_format_code_additional_empty_line_before_doc(
)

assert (
'\n\n\ndef my_func():\n"""Summary of my function."""\npass'
'def my_func():\n"""Summary of my function."""\npass'
== uut._do_format_code(
'\n\n\ndef my_func():\n\n"""Summary of my function."""\npass'
'def my_func():\n\n"""Summary of my function."""\npass'
)
)

Expand Down Expand Up @@ -1107,7 +1107,7 @@ def test_format_code_strip_blank_line_after_module_variable(
test_args,
args,
):
"""Strip newlines between module variable defintiion and docstring."""
"""Strip newlines between module variable definition and docstring."""
uut = Formatter(
test_args,
sys.stderr,
Expand Down Expand Up @@ -1157,6 +1157,34 @@ def mock_wps_request(method, url, *_, **kwargs):
'''
assert docstring == uut._do_format_code(docstring)

@pytest.mark.unit
@pytest.mark.parametrize("args", [[""]])
def test_format_code_keep_newline_after_shebang(
self,
test_args,
args,
):
"""Do not remove newlines following the shebang.
See issue #187.
"""
uut = Formatter(
test_args,
sys.stderr,
sys.stdin,
sys.stdout,
)

docstring = '''\
#!/usr/bin/env python
"""a.py."""
'''
assert docstring == uut._do_format_code('''\
#!/usr/bin/env python
"""a.py"""
''')

class TestFormatCodeRanges:
"""Class for testing _format_code() with the line_range or length_range
Expand Down

0 comments on commit 1cb6545

Please sign in to comment.