From 30c83d74ab5c8e88cdc7d5cde018cec3aca8f3d0 Mon Sep 17 00:00:00 2001 From: Adam Dangoor Date: Tue, 13 Jan 2026 04:56:21 +0000 Subject: [PATCH 1/4] Add failing tests for black formatting blank lines --- tests/_data/string_files/do_format_code.toml | 12 ++++++++++++ tests/formatter/test_do_format_code.py | 1 + 2 files changed, 13 insertions(+) diff --git a/tests/_data/string_files/do_format_code.toml b/tests/_data/string_files/do_format_code.toml index dc22f59..1b086eb 100644 --- a/tests/_data/string_files/do_format_code.toml +++ b/tests/_data/string_files/do_format_code.toml @@ -1233,3 +1233,15 @@ expected="""foo = f''' bar ''' """ + +[issue_331_black_module_docstring] +source='''""" +A +""" + +pass +''' +expected='''"""A.""" + +pass +''' diff --git a/tests/formatter/test_do_format_code.py b/tests/formatter/test_do_format_code.py index 861e439..5d04f1d 100644 --- a/tests/formatter/test_do_format_code.py +++ b/tests/formatter/test_do_format_code.py @@ -140,6 +140,7 @@ ("ellipses_is_code_line", NO_ARGS), ("do_not_break_f_string_double_quotes", NO_ARGS), ("do_not_break_f_string_single_quotes", NO_ARGS), + ("issue_331_black_module_docstring", ["--black", ""]), ], ) def test_do_format_code(test_key, test_args, args): From bb0fcff8e91c945378a34c416f8c2a75b6ea14fd Mon Sep 17 00:00:00 2001 From: Adam Dangoor Date: Tue, 13 Jan 2026 04:59:11 +0000 Subject: [PATCH 2/4] Fix support for black --- src/docformatter/format.py | 15 ++------------ tests/_data/string_files/do_format_code.toml | 5 ++--- .../_data/string_files/format_functions.toml | 14 +------------ tests/formatter/test_format_functions.py | 20 ++++--------------- 4 files changed, 9 insertions(+), 45 deletions(-) diff --git a/src/docformatter/format.py b/src/docformatter/format.py index 66ae640..d87f865 100644 --- a/src/docformatter/format.py +++ b/src/docformatter/format.py @@ -345,25 +345,14 @@ def _get_function_docstring_newlines( # noqa: PLR0911 return 0 -def _get_module_docstring_newlines(black: bool = False) -> int: +def _get_module_docstring_newlines() -> int: """Return number of newlines after a module docstring. - docformatter_8.2: One blank line after a module docstring. - docformatter_8.2.1: Two blank lines after a module docstring when in black mode. - - Parameters - ---------- - black : bool - Indicates whether we're using black formatting rules. - Returns ------- newlines : int The number of newlines to insert after the docstring. """ - if black: - return 2 - return 1 @@ -395,7 +384,7 @@ def _get_newlines_by_type( return 0 elif _classify.is_module_docstring(tokens, index): # print("Module") - return _get_module_docstring_newlines(black) + return _get_module_docstring_newlines() elif _classify.is_class_docstring(tokens, index): # print("Class") return _get_class_docstring_newlines(tokens, index) diff --git a/tests/_data/string_files/do_format_code.toml b/tests/_data/string_files/do_format_code.toml index 1b086eb..5dbd8ec 100644 --- a/tests/_data/string_files/do_format_code.toml +++ b/tests/_data/string_files/do_format_code.toml @@ -1235,9 +1235,8 @@ expected="""foo = f''' """ [issue_331_black_module_docstring] -source='''""" -A -""" +source='''"""A.""" + pass ''' diff --git a/tests/_data/string_files/format_functions.toml b/tests/_data/string_files/format_functions.toml index 0735b77..21c91e8 100644 --- a/tests/_data/string_files/format_functions.toml +++ b/tests/_data/string_files/format_functions.toml @@ -2,21 +2,9 @@ # [type, string, start, end, line] # for creating a TokenInfo() object. -[module_docstring_followed_by_string] +[module_docstring] expected = 1 -[module_docstring_followed_by_code] -expected = 1 - -[module_docstring_followed_by_comment_then_code] -expected = 1 - -[module_docstring_followed_by_comment_then_string] -expected = 1 - -[module_docstring_in_black] -expected = 2 - [class_docstring_followed_by_statement] source = ''' class MyClass: diff --git a/tests/formatter/test_format_functions.py b/tests/formatter/test_format_functions.py index 6456da7..a81f0a5 100644 --- a/tests/formatter/test_format_functions.py +++ b/tests/formatter/test_format_functions.py @@ -63,23 +63,11 @@ def _get_docstring_token_and_index(tokens): @pytest.mark.unit -@pytest.mark.parametrize( - "test_key, black", - [ - ("module_docstring_followed_by_string", False), - ("module_docstring_followed_by_code", False), - ("module_docstring_followed_by_comment_then_code", False), - ("module_docstring_followed_by_comment_then_string", False), - ("module_docstring_in_black", True), - ], -) -def test_module_docstring_newlines(test_key, black): - expected = TEST_STRINGS[test_key]["expected"] +def test_module_docstring_newlines(): + expected = TEST_STRINGS["module_docstring"]["expected"] - result = _format._get_module_docstring_newlines(black) - assert ( - result == expected - ), f"\nFailed {test_key}:\nExpected {expected}\nGot {result}" + result = _format._get_module_docstring_newlines() + assert result == expected, f"\nExpected {expected}\nGot {result}" @pytest.mark.unit From 8038f7ebba9de6c357d5fbd0aac6dd471c9cad9f Mon Sep 17 00:00:00 2001 From: Adam Dangoor Date: Tue, 13 Jan 2026 05:05:53 +0000 Subject: [PATCH 3/4] Revert a lot of test changes --- tests/_data/string_files/format_functions.toml | 14 +++++++++++++- tests/formatter/test_format_functions.py | 18 +++++++++++++++--- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/tests/_data/string_files/format_functions.toml b/tests/_data/string_files/format_functions.toml index 21c91e8..36e3a9e 100644 --- a/tests/_data/string_files/format_functions.toml +++ b/tests/_data/string_files/format_functions.toml @@ -2,7 +2,19 @@ # [type, string, start, end, line] # for creating a TokenInfo() object. -[module_docstring] +[module_docstring_followed_by_string] +expected = 1 + +[module_docstring_followed_by_code] +expected = 1 + +[module_docstring_followed_by_comment_then_code] +expected = 1 + +[module_docstring_followed_by_comment_then_string] +expected = 1 + +[module_docstring_in_black] expected = 1 [class_docstring_followed_by_statement] diff --git a/tests/formatter/test_format_functions.py b/tests/formatter/test_format_functions.py index a81f0a5..708edb4 100644 --- a/tests/formatter/test_format_functions.py +++ b/tests/formatter/test_format_functions.py @@ -63,11 +63,23 @@ def _get_docstring_token_and_index(tokens): @pytest.mark.unit -def test_module_docstring_newlines(): - expected = TEST_STRINGS["module_docstring"]["expected"] +@pytest.mark.parametrize( + "test_key", + [ + "module_docstring_followed_by_string", + "module_docstring_followed_by_code", + "module_docstring_followed_by_comment_then_code", + "module_docstring_followed_by_comment_then_string", + "module_docstring_in_black", + ], +) +def test_module_docstring_newlines(test_key): + expected = TEST_STRINGS[test_key]["expected"] result = _format._get_module_docstring_newlines() - assert result == expected, f"\nExpected {expected}\nGot {result}" + assert ( + result == expected + ), f"\nFailed {test_key}:\nExpected {expected}\nGot {result}" @pytest.mark.unit From 9bb39cfa77c487030d60d278a1964606bd9209fa Mon Sep 17 00:00:00 2001 From: Adam Dangoor Date: Wed, 14 Jan 2026 18:45:49 +0000 Subject: [PATCH 4/4] Remove unnecessary _get_newlines_by_type black parameter --- src/docformatter/format.py | 9 +++------ tests/formatter/test_format_functions.py | 16 ++++++++-------- 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/src/docformatter/format.py b/src/docformatter/format.py index d87f865..bceabd1 100644 --- a/src/docformatter/format.py +++ b/src/docformatter/format.py @@ -26,7 +26,6 @@ # SOFTWARE. """This module provides docformatter's Formattor class.""" - # Standard Library Imports import argparse import collections @@ -359,7 +358,6 @@ def _get_module_docstring_newlines() -> int: def _get_newlines_by_type( tokens: list[tokenize.TokenInfo], index: int, - black: bool = False, ) -> int: """Dispatch to the correct docstring formatter based on context. @@ -969,9 +967,7 @@ def _do_format_oneline_docstring( ).strip() if self.args.close_quotes_on_newline and "\n" in summary_wrapped: summary_wrapped = ( - f"{summary_wrapped[:-3]}" - f"\n{indentation}" - f"{summary_wrapped[-3:]}" + f"{summary_wrapped[:-3]}\n{indentation}{summary_wrapped[-3:]}" ) return summary_wrapped @@ -1058,7 +1054,8 @@ def _do_rewrite_docstring_blocks( _docstring_token = tokens[_docstr_idx] _blank_line_count = _get_newlines_by_type( - tokens, _docstr_idx, black=self.args.black + tokens, + _docstr_idx, ) if ( diff --git a/tests/formatter/test_format_functions.py b/tests/formatter/test_format_functions.py index 708edb4..5afd39e 100644 --- a/tests/formatter/test_format_functions.py +++ b/tests/formatter/test_format_functions.py @@ -221,23 +221,23 @@ def test_do_remove_preceding_blank_lines(test_key, block): @pytest.mark.integration @pytest.mark.order(5) @pytest.mark.parametrize( - "test_key, black", + "test_key", [ - ("get_newlines_by_type_module_docstring", False), - ("get_newlines_by_type_module_docstring_black", True), - ("get_newlines_by_type_class_docstring", False), - ("get_newlines_by_type_function_docstring", False), - ("get_newlines_by_type_attribute_docstring", False), + "get_newlines_by_type_module_docstring", + "get_newlines_by_type_module_docstring_black", + "get_newlines_by_type_class_docstring", + "get_newlines_by_type_function_docstring", + "get_newlines_by_type_attribute_docstring", ], ) -def test_get_newlines_by_type(test_key, black): +def test_get_newlines_by_type(test_key): source = TEST_STRINGS[test_key]["source"] expected = TEST_STRINGS[test_key]["expected"] tokens = _get_tokens(source) index = _get_docstring_token_and_index(tokens) - result = _format._get_newlines_by_type(tokens, index, black) + result = _format._get_newlines_by_type(tokens, index) assert result == expected, f"\nFailed {test_key}\nExpected {expected}\nGot {result}"