Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No empty lines before class and method docstrings #131

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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