diff --git a/.github/workflows/on-issue-open.yml b/.github/workflows/on-issue-open.yml index 491c5f2..568dcaa 100644 --- a/.github/workflows/on-issue-open.yml +++ b/.github/workflows/on-issue-open.yml @@ -2,7 +2,7 @@ name: Issue Open Workflow on: issues: - types: [opened, edited] + types: [opened] jobs: label_issue_backlog: diff --git a/src/docformatter/format.py b/src/docformatter/format.py index 072b71a..651d0fb 100644 --- a/src/docformatter/format.py +++ b/src/docformatter/format.py @@ -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 diff --git a/tests/test_format_code.py b/tests/test_format_code.py index 9f233d6..7a663d9 100644 --- a/tests/test_format_code.py +++ b/tests/test_format_code.py @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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( @@ -1245,7 +1282,8 @@ def test_method_2(self): pass -''') +''' + ) class TestFormatCodeRanges: @@ -1480,4 +1518,4 @@ def new_function(): return "\n".join(split[found:]) ''' - assert docstring == uut._do_format_code(docstring) \ No newline at end of file + assert docstring == uut._do_format_code(docstring)