-
-
Notifications
You must be signed in to change notification settings - Fork 394
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Limit empty lines in functions or methods body (#2487)
* add class and test for restrict all empty lines in func body * add test case: function with comments * add configuration param * fix baseline for formatting violation * self.generic_visit(node) in visitor * rename WrongEmptyLinesCountVisitorViolation -> WrongEmptyLinesCountViolation * 0 available empty lines case * update CHANGELOG * Fix tests * Fix algorithm for calculating expression lines * Update tests/test_visitors/test_ast/test_classes/test_wrong_empty_lines_count.py fix typo Co-authored-by: Łukasz Skarżyński <skarzynski_lukasz@protonmail.com> * Update wemake_python_styleguide/options/config.py fix typo Co-authored-by: Łukasz Skarżyński <skarzynski_lukasz@protonmail.com> * Update wemake_python_styleguide/options/defaults.py fix typo Co-authored-by: Łukasz Skarżyński <skarzynski_lukasz@protonmail.com> * Update wemake_python_styleguide/violations/best_practices.py Co-authored-by: Łukasz Skarżyński <skarzynski_lukasz@protonmail.com> * Update wemake_python_styleguide/violations/best_practices.py Co-authored-by: Łukasz Skarżyński <skarzynski_lukasz@protonmail.com> * Copying set by method Co-authored-by: Łukasz Skarżyński <skarzynski_lukasz@protonmail.com> * Optimize visitor Co-authored-by: Łukasz Skarżyński <skarzynski_lukasz@protonmail.com> * Update wemake_python_styleguide/visitors/ast/function_empty_lines.py Co-authored-by: Łukasz Skarżyński <skarzynski_lukasz@protonmail.com> * Fix typos and parse_ast_tree argument * change visitor to BaseTokenVisitor * test option * test cases with docstring and comments * fix error template Co-authored-by: Nikita Sobolev <mail@sobolevn.me> * expanded documentation Co-authored-by: Nikita Sobolev <mail@sobolevn.me> * test valid configuration with allow zero empty lines Co-authored-by: Łukasz Skarżyński <skarzynski_lukasz@protonmail.com> Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
- Loading branch information
1 parent
5bfdbdd
commit 4ca670e
Showing
10 changed files
with
326 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
197 changes: 197 additions & 0 deletions
197
tests/test_visitors/test_ast/test_classes/test_wrong_empty_lines_count.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = """ | ||
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 | ||
""" | ||
|
||
|
||
@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( | ||
options(exps_for_one_empty_line=0), file_tokens=file_tokens, | ||
) | ||
visitor.run() | ||
assert_errors(visitor, [WrongEmptyLinesCountViolation]) | ||
|
||
|
||
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, []) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.