Skip to content

Commit 4754c77

Browse files
adamtheturtleweibullguyclaude
authored
Match spacing after module docstring between black and --black (#335)
* Add failing tests for black formatting blank lines * Fix support for black * Revert a lot of test changes * Remove unnecessary _get_newlines_by_type black parameter * docs: address review feedback on module docstring helper Restore the docformatter_8.2 requirement reference and drop the stale black parameter description from _get_newlines_by_type. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Doyle Rowland <doyle.rowland@reliaqual.com> Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent d08ebe3 commit 4754c77

File tree

5 files changed

+30
-46
lines changed

5 files changed

+30
-46
lines changed

src/docformatter/format.py

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
# SOFTWARE.
2727
"""This module provides docformatter's Formattor class."""
2828

29-
3029
# Standard Library Imports
3130
import argparse
3231
import collections
@@ -381,32 +380,22 @@ def _get_function_docstring_newlines( # noqa: PLR0911
381380
return 0
382381

383382

384-
def _get_module_docstring_newlines(black: bool = False) -> int:
383+
def _get_module_docstring_newlines() -> int:
385384
"""Return number of newlines after a module docstring.
386385
387386
docformatter_8.2: One blank line after a module docstring.
388-
docformatter_8.2.1: Two blank lines after a module docstring when in black mode.
389-
390-
Parameters
391-
----------
392-
black : bool
393-
Indicates whether we're using black formatting rules.
394387
395388
Returns
396389
-------
397390
newlines : int
398391
The number of newlines to insert after the docstring.
399392
"""
400-
if black:
401-
return 2
402-
403393
return 1
404394

405395

406396
def _get_newlines_by_type(
407397
tokens: list[tokenize.TokenInfo],
408398
index: int,
409-
black: bool = False,
410399
) -> int:
411400
"""Dispatch to the correct docstring formatter based on context.
412401
@@ -418,8 +407,6 @@ def _get_newlines_by_type(
418407
A list of tokens from the source code.
419408
index : int
420409
The index of the docstring token in the list of tokens.
421-
black : bool
422-
Whether docformatter is running in black mode.
423410
424411
Returns
425412
-------
@@ -431,7 +418,7 @@ def _get_newlines_by_type(
431418
return 0
432419
elif _classify.is_module_docstring(tokens, index):
433420
# print("Module")
434-
return _get_module_docstring_newlines(black)
421+
return _get_module_docstring_newlines()
435422
elif _classify.is_class_docstring(tokens, index):
436423
# print("Class")
437424
return _get_class_docstring_newlines(tokens, index)
@@ -1021,9 +1008,7 @@ def _do_format_oneline_docstring(
10211008
).strip()
10221009
if self.args.close_quotes_on_newline and "\n" in summary_wrapped:
10231010
summary_wrapped = (
1024-
f"{summary_wrapped[:-3]}"
1025-
f"\n{indentation}"
1026-
f"{summary_wrapped[-3:]}"
1011+
f"{summary_wrapped[:-3]}\n{indentation}{summary_wrapped[-3:]}"
10271012
)
10281013
return summary_wrapped
10291014

@@ -1110,7 +1095,8 @@ def _do_rewrite_docstring_blocks(
11101095

11111096
_docstring_token = tokens[_docstr_idx]
11121097
_blank_line_count = _get_newlines_by_type(
1113-
tokens, _docstr_idx, black=self.args.black
1098+
tokens,
1099+
_docstr_idx,
11141100
)
11151101

11161102
if (

tests/_data/string_files/do_format_code.toml

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1234,15 +1234,13 @@ expected="""foo = f'''
12341234
'''
12351235
"""
12361236

1237+
[issue_331_black_module_docstring]
1238+
source='''"""A."""
12371239
1238-
[do_not_break_multiline_parameter]
1239-
source='''foo = textwrap.dedent("""\
1240-
bar
1241-
baz
1242-
""")
1240+
1241+
pass
12431242
'''
1244-
expected='''foo = textwrap.dedent("""\
1245-
bar
1246-
baz
1247-
""")
1243+
expected='''"""A."""
1244+
1245+
pass
12481246
'''

tests/_data/string_files/format_functions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ expected = 1
1515
expected = 1
1616

1717
[module_docstring_in_black]
18-
expected = 2
18+
expected = 1
1919

2020
[class_docstring_followed_by_statement]
2121
source = '''

tests/formatter/test_do_format_code.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@
140140
("ellipses_is_code_line", NO_ARGS),
141141
("do_not_break_f_string_double_quotes", NO_ARGS),
142142
("do_not_break_f_string_single_quotes", NO_ARGS),
143-
("do_not_break_multiline_parameter", NO_ARGS),
143+
("issue_331_black_module_docstring", ["--black", ""]),
144144
],
145145
)
146146
def test_do_format_code(test_key, test_args, args):

tests/formatter/test_format_functions.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -64,19 +64,19 @@ def _get_docstring_token_and_index(tokens):
6464

6565
@pytest.mark.unit
6666
@pytest.mark.parametrize(
67-
"test_key, black",
67+
"test_key",
6868
[
69-
("module_docstring_followed_by_string", False),
70-
("module_docstring_followed_by_code", False),
71-
("module_docstring_followed_by_comment_then_code", False),
72-
("module_docstring_followed_by_comment_then_string", False),
73-
("module_docstring_in_black", True),
69+
"module_docstring_followed_by_string",
70+
"module_docstring_followed_by_code",
71+
"module_docstring_followed_by_comment_then_code",
72+
"module_docstring_followed_by_comment_then_string",
73+
"module_docstring_in_black",
7474
],
7575
)
76-
def test_module_docstring_newlines(test_key, black):
76+
def test_module_docstring_newlines(test_key):
7777
expected = TEST_STRINGS[test_key]["expected"]
7878

79-
result = _format._get_module_docstring_newlines(black)
79+
result = _format._get_module_docstring_newlines()
8080
assert (
8181
result == expected
8282
), f"\nFailed {test_key}:\nExpected {expected}\nGot {result}"
@@ -221,23 +221,23 @@ def test_do_remove_preceding_blank_lines(test_key, block):
221221
@pytest.mark.integration
222222
@pytest.mark.order(5)
223223
@pytest.mark.parametrize(
224-
"test_key, black",
224+
"test_key",
225225
[
226-
("get_newlines_by_type_module_docstring", False),
227-
("get_newlines_by_type_module_docstring_black", True),
228-
("get_newlines_by_type_class_docstring", False),
229-
("get_newlines_by_type_function_docstring", False),
230-
("get_newlines_by_type_attribute_docstring", False),
226+
"get_newlines_by_type_module_docstring",
227+
"get_newlines_by_type_module_docstring_black",
228+
"get_newlines_by_type_class_docstring",
229+
"get_newlines_by_type_function_docstring",
230+
"get_newlines_by_type_attribute_docstring",
231231
],
232232
)
233-
def test_get_newlines_by_type(test_key, black):
233+
def test_get_newlines_by_type(test_key):
234234
source = TEST_STRINGS[test_key]["source"]
235235
expected = TEST_STRINGS[test_key]["expected"]
236236

237237
tokens = _get_tokens(source)
238238
index = _get_docstring_token_and_index(tokens)
239239

240-
result = _format._get_newlines_by_type(tokens, index, black)
240+
result = _format._get_newlines_by_type(tokens, index)
241241
assert result == expected, f"\nFailed {test_key}\nExpected {expected}\nGot {result}"
242242

243243

0 commit comments

Comments
 (0)