Skip to content

Commit

Permalink
No empty lines before class and method docstrings (#131)
Browse files Browse the repository at this point in the history
* remove blank lines before class and method docstrings

* add unit tests
  • Loading branch information
mcflugen committed Dec 9, 2022
1 parent 3b73dc5 commit 682a09d
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/docformatter/format.py
Expand Up @@ -320,7 +320,9 @@ def _format_code(
not in {tokenize.NL, tokenize.NEWLINE}
or modified_tokens[-2][1] != ":"
and modified_tokens[-2][0] != tokenize.COMMENT
or modified_tokens[-2][4][:3] != "def"
or not modified_tokens[-2][4]
.lstrip()
.startswith(("def", "class"))
):
modified_tokens.append(
(token_type, token_string, start, end, line)
Expand Down
80 changes: 78 additions & 2 deletions tests/test_format_code.py
Expand Up @@ -499,6 +499,80 @@ def foo():
'''
)

@pytest.mark.unit
@pytest.mark.parametrize("args", [[""]])
def test_format_code_with_empty_lines_class_docstring(
self, test_args, args
):
"""No blank lines before a class's docstring."""
uut = Formatter(
test_args,
sys.stderr,
sys.stdin,
sys.stdout,
)

assert '''\
class Foo:
"""Hello foo and this is a docstring.
More stuff.
"""
''' == uut._format_code(
'''\
class Foo:
"""
Hello
foo and this is a docstring.
More stuff.
"""
'''
)
assert '''\
def foo():
class Foo:
"""Summary."""
pass
''' == uut._format_code(
'''\
def foo():
class Foo:
"""Summary."""
pass
'''
)

@pytest.mark.unit
@pytest.mark.parametrize("args", [[""]])
def test_format_code_with_empty_lines_method_docstring(
self, test_args, args
):
"""No blank lines before a method's docstring."""
uut = Formatter(
test_args,
sys.stderr,
sys.stdin,
sys.stdout,
)

assert '''\
class Foo:
def foo(self):
"""Summary."""
pass
''' == uut._format_code(
'''\
class Foo:
def foo(self):
"""Summary."""
pass
'''
)

@pytest.mark.unit
@pytest.mark.parametrize("args", [[""]])
def test_format_code_with_trailing_whitespace(self, test_args, args):
Expand Down Expand Up @@ -860,13 +934,15 @@ class TestClass:
..py.method: big_method()
"""
'''
assert docstring == uut._do_format_code('''\
assert docstring == uut._do_format_code(
'''\
class TestClass:
"""This is a class docstring.
:cvar test_int: a class attribute.
..py.method: big_method()
"""
''')
'''
)


class TestFormatCodeRanges:
Expand Down

0 comments on commit 682a09d

Please sign in to comment.