Skip to content

Commit 1cb6545

Browse files
authored
fix: removing newline after shebang (#188)
* fix: removing newline after shebang * test: for removing newline after shebang
1 parent 1e9076a commit 1cb6545

File tree

3 files changed

+50
-16
lines changed

3 files changed

+50
-16
lines changed

src/docformatter/format.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -528,14 +528,20 @@ def _do_remove_blank_lines_after_definitions(modified_tokens):
528528
"""
529529
for _idx, _token in enumerate(modified_tokens):
530530
if _token[0] == 3:
531-
# Remove newline between variable definition and docstring.
532531
j = 1
533-
while modified_tokens[_idx - j][
534-
4
535-
] == "\n" and not modified_tokens[_idx - j - 1][
536-
4
537-
].strip().endswith(
538-
'"""'
532+
533+
# Remove newline between variable definition and docstring
534+
# unless is separating docstring from:
535+
# * A previous docstring.
536+
# * The file's shebang.
537+
while (
538+
modified_tokens[_idx - j][4] == "\n"
539+
and not (
540+
modified_tokens[_idx - j - 1][4]
541+
.strip()
542+
.endswith('"""')
543+
)
544+
and not modified_tokens[_idx - j - 1][4].startswith("#!/")
539545
):
540546
modified_tokens.pop(_idx - j)
541547
j += 1

src/docformatter/strings.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,7 @@ def normalize_summary(summary: str) -> str:
104104
# Remove trailing whitespace
105105
summary = summary.rstrip()
106106

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

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

123123
return summary

tests/test_format_code.py

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -853,9 +853,9 @@ def test_format_code_additional_empty_line_before_doc(
853853
)
854854

855855
assert (
856-
'\n\n\ndef my_func():\n"""Summary of my function."""\npass'
856+
'def my_func():\n"""Summary of my function."""\npass'
857857
== uut._do_format_code(
858-
'\n\n\ndef my_func():\n\n"""Summary of my function."""\npass'
858+
'def my_func():\n\n"""Summary of my function."""\npass'
859859
)
860860
)
861861

@@ -1107,7 +1107,7 @@ def test_format_code_strip_blank_line_after_module_variable(
11071107
test_args,
11081108
args,
11091109
):
1110-
"""Strip newlines between module variable defintiion and docstring."""
1110+
"""Strip newlines between module variable definition and docstring."""
11111111
uut = Formatter(
11121112
test_args,
11131113
sys.stderr,
@@ -1157,6 +1157,34 @@ def mock_wps_request(method, url, *_, **kwargs):
11571157
'''
11581158
assert docstring == uut._do_format_code(docstring)
11591159

1160+
@pytest.mark.unit
1161+
@pytest.mark.parametrize("args", [[""]])
1162+
def test_format_code_keep_newline_after_shebang(
1163+
self,
1164+
test_args,
1165+
args,
1166+
):
1167+
"""Do not remove newlines following the shebang.
1168+
1169+
See issue #187.
1170+
"""
1171+
uut = Formatter(
1172+
test_args,
1173+
sys.stderr,
1174+
sys.stdin,
1175+
sys.stdout,
1176+
)
1177+
1178+
docstring = '''\
1179+
#!/usr/bin/env python
1180+
1181+
"""a.py."""
1182+
'''
1183+
assert docstring == uut._do_format_code('''\
1184+
#!/usr/bin/env python
1185+
1186+
"""a.py"""
1187+
''')
11601188

11611189
class TestFormatCodeRanges:
11621190
"""Class for testing _format_code() with the line_range or length_range

0 commit comments

Comments
 (0)