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

fix: removing newline after shebang #188

Merged
merged 2 commits into from
Apr 25, 2023
Merged
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
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