-
-
Notifications
You must be signed in to change notification settings - Fork 385
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
Limit empty lines in functions or methods body #2487
Changes from all commits
e48a050
7136d32
9bf523d
cf78379
8b490ab
1715c0f
9e4943e
b11609c
cb34ca8
55ba64c
f913f44
43a88b5
88cdb63
36b74d2
d78bb99
7a48d2f
e409a41
2bfc662
57d8696
e0370f9
4fda3be
b0cfd96
3de1283
919dc2a
96bf0ee
62ab4b9
be93737
8f366b7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,197 @@ | ||
"""Test method contain only allowed empty lines count.""" | ||
import pytest | ||
|
||
from wemake_python_styleguide.violations.best_practices import ( | ||
WrongEmptyLinesCountViolation, | ||
) | ||
from wemake_python_styleguide.visitors.ast.function_empty_lines import ( | ||
WrongEmptyLinesCountVisitor, | ||
) | ||
|
||
class_with_wrong_method = """ | ||
class WrongClass(object): | ||
|
||
def wrong_method(self): | ||
foo() | ||
|
||
bar() | ||
|
||
baz() | ||
|
||
lighter() | ||
""" | ||
|
||
class_with_valid_method = """ | ||
class WrongClass(object): | ||
|
||
def wrong_method(self): | ||
foo() | ||
bar() | ||
baz() | ||
""" | ||
|
||
wrong_function = """ | ||
def func(): | ||
foo() | ||
|
||
a = 1 + 4 | ||
|
||
bar() | ||
|
||
baz() | ||
""" | ||
|
||
wrong_function_with_loop = """ | ||
def func(): | ||
for x in range(10): | ||
|
||
requests.get( | ||
|
||
|
||
'https://github.com/wemake-services/wemake-python-styleguide' | ||
) | ||
""" | ||
|
||
allow_function = """ | ||
def func(): | ||
foo() | ||
if name == 'Moonflower': | ||
print('Love') | ||
|
||
baz() | ||
""" | ||
|
||
allow_function_with_comments = """ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please, add this case: def test_func():
"""
Its docstring
has many new lines
but this is
totally fine
we don't raise a violation for this
"""
return |
||
def test_func(): | ||
# This function | ||
# | ||
# has lots | ||
# | ||
# of empty | ||
# | ||
# lines | ||
# | ||
# in comments | ||
return 0 | ||
""" | ||
|
||
function_with_docstring = """ | ||
def test_func(): | ||
\""" | ||
Its docstring | ||
|
||
has many new lines | ||
|
||
but this is | ||
|
||
totally fine | ||
|
||
we don't raise a violation for this | ||
\""" | ||
return | ||
""" | ||
|
||
function_with_docstring_and_comments = """ | ||
def test_func(): | ||
\""" | ||
Its docstring | ||
|
||
has many new lines | ||
|
||
but this is | ||
|
||
totally fine | ||
|
||
we don't raise a violation for this | ||
\""" | ||
# This function | ||
# | ||
# has lots | ||
# | ||
# of empty | ||
# | ||
# lines | ||
# | ||
# in comments | ||
return 0 | ||
""" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We also need a test case like this: def test_func():
# This function
#
# has lots
#
# of empty
#
# lines
#
# in comments
return 0 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A mix of comments and docstring is also a good idea 👍 |
||
|
||
|
||
@pytest.mark.parametrize('input_', [ | ||
class_with_wrong_method, | ||
wrong_function, | ||
wrong_function_with_loop, | ||
]) | ||
def test_wrong( | ||
input_, | ||
default_options, | ||
assert_errors, | ||
parse_tokens, | ||
mode, | ||
): | ||
"""Testing wrong cases.""" | ||
file_tokens = parse_tokens(mode(input_)) | ||
|
||
visitor = WrongEmptyLinesCountVisitor( | ||
default_options, file_tokens=file_tokens, | ||
) | ||
visitor.run() | ||
|
||
assert_errors(visitor, [WrongEmptyLinesCountViolation]) | ||
|
||
|
||
@pytest.mark.parametrize('input_', [ | ||
class_with_valid_method, | ||
allow_function, | ||
allow_function_with_comments, | ||
function_with_docstring, | ||
function_with_docstring_and_comments, | ||
]) | ||
def test_success( | ||
input_, | ||
parse_tokens, | ||
default_options, | ||
assert_errors, | ||
mode, | ||
): | ||
"""Testing available cases.""" | ||
file_tokens = parse_tokens(mode(input_)) | ||
|
||
visitor = WrongEmptyLinesCountVisitor( | ||
default_options, file_tokens=file_tokens, | ||
) | ||
visitor.run() | ||
|
||
assert_errors(visitor, []) | ||
|
||
|
||
def test_zero_option( | ||
parse_tokens, | ||
default_options, | ||
assert_errors, | ||
options, | ||
mode, | ||
): | ||
"""Test zero configuration.""" | ||
file_tokens = parse_tokens(mode(allow_function)) | ||
visitor = WrongEmptyLinesCountVisitor( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's also add a test that function without any empty lines do pass with |
||
options(exps_for_one_empty_line=0), file_tokens=file_tokens, | ||
) | ||
visitor.run() | ||
assert_errors(visitor, [WrongEmptyLinesCountViolation]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this assertion correct? It seems like in this test There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I forgot to change |
||
|
||
|
||
def test_zero_option_with_valid_method( | ||
parse_tokens, | ||
default_options, | ||
assert_errors, | ||
options, | ||
mode, | ||
): | ||
"""Test zero configuration with valid method.""" | ||
file_tokens = parse_tokens(mode(class_with_valid_method)) | ||
visitor = WrongEmptyLinesCountVisitor( | ||
options(exps_for_one_empty_line=0), file_tokens=file_tokens, | ||
) | ||
visitor.run() | ||
assert_errors(visitor, []) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about edge cases like below ones?
Can they be handled by the
ast
?Probably this violation should be implemented by using
BaseTokenVisitor
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeh, I plan to change visitor to
BaseTokenVisitor
becauseast
ignore comments. I check these cases after changing the visitor, thank you