Skip to content

Commit 4c6fdc6

Browse files
authored
fix: removing blank line after import section (#204)
* fix: removing blank line after import section * test: for removing blank line after import section
1 parent 775a0e7 commit 4c6fdc6

File tree

3 files changed

+60
-20
lines changed

3 files changed

+60
-20
lines changed

.github/workflows/on-issue-open.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: Issue Open Workflow
22

33
on:
44
issues:
5-
types: [opened, edited]
5+
types: [opened]
66

77
jobs:
88
label_issue_backlog:

src/docformatter/format.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,14 @@ def _do_remove_blank_lines_after_definitions(
7272
# unless it's separating a docstring from:
7373
# * A previous docstring.
7474
# * The file's shebang.
75+
# * The import section.
7576
while (
7677
modified_tokens[_idx - j][4] == "\n"
7778
and not (
7879
modified_tokens[_idx - j - 1][4].strip().endswith('"""')
7980
)
8081
and not modified_tokens[_idx - j - 1][4].startswith("#!/")
82+
and "import" not in modified_tokens[_idx - j - 1][4]
8183
):
8284
modified_tokens.pop(_idx - j)
8385
j += 1

tests/test_format_code.py

Lines changed: 57 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,7 @@ def test_method_no_chr_92(): the501(92) # \
213213
def test_format_code_raw_docstring_double_quotes(self, test_args, args):
214214
"""Should format raw docstrings with triple double quotes.
215215
216-
See requirement PEP_257_2. See issue #54 for request to handle
217-
raw docstrings.
216+
See requirement PEP_257_2. See issue #54 for request to handle raw docstrings.
218217
"""
219218
uut = Formatter(
220219
test_args,
@@ -252,8 +251,7 @@ def foo():
252251
def test_format_code_raw_docstring_single_quotes(self, test_args, args):
253252
"""Should format raw docstrings with triple single quotes.
254253
255-
See requirement PEP_257_2. See issue #54 for request to handle
256-
raw docstrings.
254+
See requirement PEP_257_2. See issue #54 for request to handle raw docstrings.
257255
"""
258256
uut = Formatter(
259257
test_args,
@@ -293,8 +291,7 @@ def test_format_code_unicode_docstring_double_quotes(
293291
):
294292
"""Should format unicode docstrings with triple double quotes.
295293
296-
See requirement PEP_257_3. See issue #54 for request to handle
297-
raw docstrings.
294+
See requirement PEP_257_3. See issue #54 for request to handle raw docstrings.
298295
"""
299296
uut = Formatter(
300297
test_args,
@@ -334,8 +331,7 @@ def test_format_code_unicode_docstring_single_quotes(
334331
):
335332
"""Should format unicode docstrings with triple single quotes.
336333
337-
See requirement PEP_257_3. See issue #54 for request to handle
338-
raw docstrings.
334+
See requirement PEP_257_3. See issue #54 for request to handle raw docstrings.
339335
"""
340336
uut = Formatter(
341337
test_args,
@@ -639,8 +635,8 @@ def foo():
639635
def test_ignore_code_with_single_quote(self, test_args, args):
640636
"""Single single quote on first line of code should remain untouched.
641637
642-
See requirement PEP_257_1. See issue #66 for example of
643-
docformatter breaking code when encountering single quote.
638+
See requirement PEP_257_1. See issue #66 for example of docformatter breaking
639+
code when encountering single quote.
644640
"""
645641
uut = Formatter(
646642
test_args,
@@ -664,8 +660,8 @@ def foo():
664660
def test_ignore_code_with_double_quote(self, test_args, args):
665661
"""Single double quotes on first line of code should remain untouched.
666662
667-
See requirement PEP_257_1. See issue #66 for example of
668-
docformatter breaking code when encountering single quote.
663+
See requirement PEP_257_1. See issue #66 for example of docformatter breaking
664+
code when encountering single quote.
669665
"""
670666
uut = Formatter(
671667
test_args,
@@ -1180,18 +1176,59 @@ def test_format_code_keep_newline_after_shebang(
11801176
11811177
"""a.py."""
11821178
'''
1183-
assert docstring == uut._do_format_code('''\
1179+
assert docstring == uut._do_format_code(
1180+
'''\
11841181
#!/usr/bin/env python
11851182
11861183
"""a.py"""
1187-
''')
1184+
'''
1185+
)
1186+
1187+
@pytest.mark.unit
1188+
@pytest.mark.parametrize("args", [[""]])
1189+
def test_format_code_keep_newline_after_import(
1190+
self,
1191+
test_args,
1192+
args,
1193+
):
1194+
"""Do not remove newlines following the import section.
1195+
1196+
See issue #203.
1197+
"""
1198+
uut = Formatter(
1199+
test_args,
1200+
sys.stderr,
1201+
sys.stdin,
1202+
sys.stdout,
1203+
)
1204+
1205+
docstring = '''\
1206+
#!/usr/bin/env python
1207+
1208+
import os
1209+
from typing import Iterator
1210+
1211+
"""Don't remove this comment, it's cool."""
1212+
IMPORTANT_CONSTANT = "potato"
1213+
'''
1214+
assert docstring == uut._do_format_code(
1215+
'''\
1216+
#!/usr/bin/env python
1217+
1218+
import os
1219+
from typing import Iterator
1220+
1221+
"""Don't remove this comment, it's cool."""
1222+
IMPORTANT_CONSTANT = "potato"
1223+
'''
1224+
)
11881225

11891226
@pytest.mark.unit
11901227
@pytest.mark.parametrize("args", [["--black", ""]])
11911228
def test_format_code_strip_blank_line_for_black(
1192-
self,
1193-
test_args,
1194-
args,
1229+
self,
1230+
test_args,
1231+
args,
11951232
):
11961233
"""Blank lines are stripped in black mode."""
11971234
uut = Formatter(
@@ -1245,7 +1282,8 @@ def test_method_2(self):
12451282
12461283
12471284
pass
1248-
''')
1285+
'''
1286+
)
12491287

12501288

12511289
class TestFormatCodeRanges:
@@ -1480,4 +1518,4 @@ def new_function():
14801518
14811519
return "\n".join(split[found:])
14821520
'''
1483-
assert docstring == uut._do_format_code(docstring)
1521+
assert docstring == uut._do_format_code(docstring)

0 commit comments

Comments
 (0)