-
-
Notifications
You must be signed in to change notification settings - Fork 383
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
Limit empty lines in functions or methods body #2487
Conversation
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.
Please, add configuration and default ratio (I think that default amount of empty lines should be 1-for-2 non-empty lines).
@sobolevn I don't agree or don't correctly understand you proportions. In this proportions linter don't reject next code: def func(a, b):
first_expression(a)
second_expression(b) Or you implied 2 line with expressions without gap between them? |
e686c19
to
a00ff60
Compare
We can add a lower bound of 0 empty lines for 2 lines. That's not a problem. |
@sobolevn I'm pushed commit with code for configurating this rule. Please, check it |
Can you please rebase your work? I've fixed new poetry@1.2.0 build |
58ece1e
to
9e4943e
Compare
Yes, of course. My branch rebased now |
Can you please fix tests in https://github.com/wemake-services/wemake-python-styleguide/runs/8301866244?check_suite_focus=true ? |
a30ad0b
to
cb34ca8
Compare
Sorry, my bad. I connect my visitor and found a problem: |
8e7ba4c
to
6f47f50
Compare
6f47f50
to
55ba64c
Compare
lighter() | ||
""" | ||
|
||
class_with_allow_method = """ |
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.
NIP: what do you think about using valid
instead of allow
?
class_with_valid_method
sounds better
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.
Yes, I'm agree. My english level is not high, so I'm happy with any edits
tests/test_visitors/test_ast/test_classes/test_wrong_empty_lines_count.py
Outdated
Show resolved
Hide resolved
visitor = WrongEmptyLinesCountVisitor(default_options, tree=tree) | ||
visitor.run() | ||
|
||
assert_errors(visitor, [WrongEmptyLinesCountViolation]) |
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.
Is this assertion correct? It seems like in this test input_
has only a valid number of empty lines
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.
I forgot to change parse_ast_tree
after parameterizing the tests, thanks
baz() | ||
""" | ||
|
||
wrong_function_with_loop = """ |
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?
def foo():
return (
pekaboo['attr'] == 'dummy-value'
and bar() == 'Knights that say Ni!'
or (
'attr2' in pekaboo
and not some_object.attribute.method()
)
)
pekaboo(
foo=123,
bar='abc',
)
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
because ast
ignore comments. I check these cases after changing the visitor, thank you
…es_count.py fix typo Co-authored-by: Łukasz Skarżyński <skarzynski_lukasz@protonmail.com>
fix typo Co-authored-by: Łukasz Skarżyński <skarzynski_lukasz@protonmail.com>
fix typo Co-authored-by: Łukasz Skarżyński <skarzynski_lukasz@protonmail.com>
Co-authored-by: Łukasz Skarżyński <skarzynski_lukasz@protonmail.com>
Co-authored-by: Łukasz Skarżyński <skarzynski_lukasz@protonmail.com>
Co-authored-by: Łukasz Skarżyński <skarzynski_lukasz@protonmail.com>
Co-authored-by: Łukasz Skarżyński <skarzynski_lukasz@protonmail.com>
Co-authored-by: Łukasz Skarżyński <skarzynski_lukasz@protonmail.com>
….com/blablatdinov/wemake-python-styleguide into pr-restrict-empty-lines-in-func-body
I'm change visitor to |
Codecov Report
@@ Coverage Diff @@
## master #2487 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 119 120 +1
Lines 6390 6431 +41
Branches 1449 1458 +9
=========================================
+ Hits 6390 6431 +41
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. |
""" | ||
|
||
|
||
allow_function_with_comments = """ |
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.
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
parse_tokens, | ||
): | ||
"""Testing wrong cases.""" | ||
file_tokens = parse_tokens(input_) |
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.
Don't forget to use mode
fixture to test async def
as well.
print(customer.phone) | ||
# printing customer company | ||
print(customer.company) | ||
""" |
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.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
A mix of comments and docstring is also a good idea 👍
Thanks for comments. I'm add test cases with comments and docstrings. And wrapped input code snippets into |
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.
Awesome! This is the last piece of feedback that I have.
): | ||
"""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 comment
The 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 exps_for_one_empty_line=0
Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
….com/blablatdinov/wemake-python-styleguide into pr-restrict-empty-lines-in-func-body
Thanks for your suggested changes! I added test with |
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.
Thank you for the great idea and solid implementation!
👏
I have made things!
Checklist
CHANGELOG.md
Related issues
Format is:
🙏 Please, if you or your company is finding wemake-python-styleguide valuable, help us sustain the project by sponsoring it transparently on https://opencollective.com/wemake-python-styleguide. As a thank you, your profile/company logo will be added to our main README which receives hundreds of unique visitors per day.