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 blank line after import section #204

Merged
merged 2 commits into from
May 3, 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
2 changes: 1 addition & 1 deletion .github/workflows/on-issue-open.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Issue Open Workflow

on:
issues:
types: [opened, edited]
types: [opened]

jobs:
label_issue_backlog:
Expand Down
2 changes: 2 additions & 0 deletions src/docformatter/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,14 @@ def _do_remove_blank_lines_after_definitions(
# unless it's separating a docstring from:
# * A previous docstring.
# * The file's shebang.
# * The import section.
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("#!/")
and "import" not in modified_tokens[_idx - j - 1][4]
):
modified_tokens.pop(_idx - j)
j += 1
Expand Down
76 changes: 57 additions & 19 deletions tests/test_format_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,7 @@ def test_method_no_chr_92(): the501(92) # \
def test_format_code_raw_docstring_double_quotes(self, test_args, args):
"""Should format raw docstrings with triple double quotes.

See requirement PEP_257_2. See issue #54 for request to handle
raw docstrings.
See requirement PEP_257_2. See issue #54 for request to handle raw docstrings.
"""
uut = Formatter(
test_args,
Expand Down Expand Up @@ -252,8 +251,7 @@ def foo():
def test_format_code_raw_docstring_single_quotes(self, test_args, args):
"""Should format raw docstrings with triple single quotes.

See requirement PEP_257_2. See issue #54 for request to handle
raw docstrings.
See requirement PEP_257_2. See issue #54 for request to handle raw docstrings.
"""
uut = Formatter(
test_args,
Expand Down Expand Up @@ -293,8 +291,7 @@ def test_format_code_unicode_docstring_double_quotes(
):
"""Should format unicode docstrings with triple double quotes.

See requirement PEP_257_3. See issue #54 for request to handle
raw docstrings.
See requirement PEP_257_3. See issue #54 for request to handle raw docstrings.
"""
uut = Formatter(
test_args,
Expand Down Expand Up @@ -334,8 +331,7 @@ def test_format_code_unicode_docstring_single_quotes(
):
"""Should format unicode docstrings with triple single quotes.

See requirement PEP_257_3. See issue #54 for request to handle
raw docstrings.
See requirement PEP_257_3. See issue #54 for request to handle raw docstrings.
"""
uut = Formatter(
test_args,
Expand Down Expand Up @@ -639,8 +635,8 @@ def foo():
def test_ignore_code_with_single_quote(self, test_args, args):
"""Single single quote on first line of code should remain untouched.

See requirement PEP_257_1. See issue #66 for example of
docformatter breaking code when encountering single quote.
See requirement PEP_257_1. See issue #66 for example of docformatter breaking
code when encountering single quote.
"""
uut = Formatter(
test_args,
Expand All @@ -664,8 +660,8 @@ def foo():
def test_ignore_code_with_double_quote(self, test_args, args):
"""Single double quotes on first line of code should remain untouched.

See requirement PEP_257_1. See issue #66 for example of
docformatter breaking code when encountering single quote.
See requirement PEP_257_1. See issue #66 for example of docformatter breaking
code when encountering single quote.
"""
uut = Formatter(
test_args,
Expand Down Expand Up @@ -1180,18 +1176,59 @@ def test_format_code_keep_newline_after_shebang(

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

"""a.py"""
''')
'''
)

@pytest.mark.unit
@pytest.mark.parametrize("args", [[""]])
def test_format_code_keep_newline_after_import(
self,
test_args,
args,
):
"""Do not remove newlines following the import section.

See issue #203.
"""
uut = Formatter(
test_args,
sys.stderr,
sys.stdin,
sys.stdout,
)

docstring = '''\
#!/usr/bin/env python

import os
from typing import Iterator

"""Don't remove this comment, it's cool."""
IMPORTANT_CONSTANT = "potato"
'''
assert docstring == uut._do_format_code(
'''\
#!/usr/bin/env python

import os
from typing import Iterator

"""Don't remove this comment, it's cool."""
IMPORTANT_CONSTANT = "potato"
'''
)

@pytest.mark.unit
@pytest.mark.parametrize("args", [["--black", ""]])
def test_format_code_strip_blank_line_for_black(
self,
test_args,
args,
self,
test_args,
args,
):
"""Blank lines are stripped in black mode."""
uut = Formatter(
Expand Down Expand Up @@ -1245,7 +1282,8 @@ def test_method_2(self):


pass
''')
'''
)


class TestFormatCodeRanges:
Expand Down Expand Up @@ -1480,4 +1518,4 @@ def new_function():

return "\n".join(split[found:])
'''
assert docstring == uut._do_format_code(docstring)
assert docstring == uut._do_format_code(docstring)