Skip to content

Commit 264cad5

Browse files
committed
fix: handle case where class contains only docstring
1 parent 9a441b9 commit 264cad5

File tree

4 files changed

+45
-1
lines changed

4 files changed

+45
-1
lines changed

src/docformatter/classify.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ def is_string_variable(
463463
try:
464464
_token_types = (tokenize.AWAIT, tokenize.OP)
465465
except AttributeError:
466-
_token_types = (tokenize.OP,) # type: ignore
466+
_token_types = (tokenize.OP,)
467467

468468
if prev_token.type in _token_types and (
469469
'= """' in token.line or token.line in prev_token.line

src/docformatter/format.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,11 +260,22 @@ def _get_class_docstring_newlines(
260260
The number of newlines to insert after the docstring.
261261
"""
262262
j = index + 1
263+
indention_level = tokens[index].start[1]
263264

264265
# The docstring is followed by a comment.
265266
if tokens[j].string.startswith("#"):
266267
return 0
267268

269+
while j < len(tokens):
270+
if tokens[j].type in (tokenize.NL, tokenize.NEWLINE):
271+
j += 1
272+
continue
273+
274+
if tokens[j].start[1] < indention_level:
275+
return 2
276+
277+
break
278+
268279
return 1
269280

270281

tests/_data/string_files/do_format_code.toml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,3 +1160,34 @@ expected='''def foo(bar):
11601160
Description.
11611161
"""
11621162
'''
1163+
1164+
[two_lines_between_stub_classes]
1165+
source='''class Foo:
1166+
"""Foo class."""
1167+
class Bar:
1168+
"""Bar class."""
1169+
'''
1170+
expected='''class Foo:
1171+
"""Foo class."""
1172+
1173+
1174+
class Bar:
1175+
"""Bar class."""
1176+
'''
1177+
1178+
[two_lines_between_stub_classes_with_preceding_comment]
1179+
source='''class Foo:
1180+
"""Foo class."""
1181+
1182+
# A comment for class Bar
1183+
class Bar:
1184+
"""Bar class."""
1185+
'''
1186+
expected='''class Foo:
1187+
"""Foo class."""
1188+
1189+
1190+
# A comment for class Bar
1191+
class Bar:
1192+
"""Bar class."""
1193+
'''

tests/formatter/test_do_format_code.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@
135135
("issue_187", NO_ARGS),
136136
("issue_203", NO_ARGS),
137137
("issue_243", NO_ARGS),
138+
("two_lines_between_stub_classes", NO_ARGS),
139+
("two_lines_between_stub_classes_with_preceding_comment", NO_ARGS),
138140
],
139141
)
140142
def test_do_format_code(test_key, test_args, args):

0 commit comments

Comments
 (0)