From 4c6fdc64eb372a696dc380b673c402802b757af6 Mon Sep 17 00:00:00 2001 From: Doyle Rowland Date: Wed, 3 May 2023 13:45:02 -0400 Subject: [PATCH 1/2] fix: removing blank line after import section (#204) * fix: removing blank line after import section * test: for removing blank line after import section --- .github/workflows/on-issue-open.yml | 2 +- src/docformatter/format.py | 2 + tests/test_format_code.py | 76 +++++++++++++++++++++-------- 3 files changed, 60 insertions(+), 20 deletions(-) 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) From a027ada45435655a12466a837ac20de1038145f9 Mon Sep 17 00:00:00 2001 From: Doyle Rowland Date: Wed, 3 May 2023 13:53:26 -0400 Subject: [PATCH 2/2] chore: update version strings (#205) --- docs/source/conf.py | 2 +- pyproject.toml | 2 +- src/docformatter/__pkginfo__.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/source/conf.py b/docs/source/conf.py index 09b9563..9b0aea9 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -9,7 +9,7 @@ project = 'docformatter' copyright = '2022-2023, Steven Myint' author = 'Steven Myint' -release = '1.6.4' +release = '1.6.5' # -- General configuration --------------------------------------------------- # https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration diff --git a/pyproject.toml b/pyproject.toml index 5fb8d11..00cdac8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "docformatter" -version = "1.6.4" +version = "1.6.5" description = "Formats docstrings to follow PEP 257" authors = ["Steven Myint"] maintainers = [ diff --git a/src/docformatter/__pkginfo__.py b/src/docformatter/__pkginfo__.py index 341eb74..71b9a88 100644 --- a/src/docformatter/__pkginfo__.py +++ b/src/docformatter/__pkginfo__.py @@ -23,4 +23,4 @@ # SOFTWARE. """Package information for docformatter.""" -__version__ = "1.6.4" +__version__ = "1.6.5"