-
-
Notifications
You must be signed in to change notification settings - Fork 394
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
Issue 1974 #1998
Issue 1974 #1998
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.
Thanks, that's PR with awesome changes, but it needs some polishing and few additional unit tests that check the newly introduced violation.
You can add a new test file to tests/test_visitors/test_ast/test_keywords/
.
Let's check e.g. tests/test_visitors/test_ast/test_keywords/test_raise.py
file with tests related to the raise
statement (so you need to add quite similar ones).
If you have any questions etc, ping me here 😄
…uide into issue-1974
…uide into issue-1974
Hi @skarzi. Thank you for reviewing our changes. They should be fixed now. We have integrated those changes and have added test cases. Flake8 runs perfectly on wemake-python-styleguide project. Do let us know if we need to change other things as well? |
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.
great progress, just a few things are worth adding/refactoring, before shipping this feature!
@@ -130,3 +131,22 @@ def test_bare_raise( | |||
visitor.run() | |||
|
|||
assert_errors(visitor, []) | |||
|
|||
|
|||
def test_bare_raise_no_except( |
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 add tests (or even better one parametrized test) for following cases:
- bare
raise
insideexcept
block raise SomeException()
insideexcept
blockraise SomeException()
not nested inexcept
block
Hi! I have updated my changes and fixed one of the noqa test cases as well to reflect our added changes. Not sure if I was supposed to change the test_noqa function. Do let me know if this change works out! Greatly appreciate the feedback. |
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.
Thanks a lot!
tests/test_checker/test_noqa.py
Outdated
@@ -442,7 +442,7 @@ def test_noqa_fixture(absolute_path): | |||
) | |||
stdout, _ = process.communicate() | |||
|
|||
assert stdout.count('WPS') == 0 | |||
assert stdout.count('WPS') == 1 |
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.
This should not be changed 🙂
default_options, | ||
): | ||
"""Testing that bare `raise` is not allowed without an except block.""" | ||
code = """ |
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 tend to define module-level variables (like correct_case1
and correct_case2
) and then use pytest.mark.parametrize
to write a single test for all correct cases.
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.
great work, I just have added a few more comments and change requests/suggestions
tests/fixtures/noqa/noqa.py
Outdated
|
||
def detect_bare_raise1(): | ||
"""Function to check if the bare raise is detected.""" | ||
raise |
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 add noqa
comment for the newly introduced violation and revert back test_noqa_fixture
test in test_noqa.py
raise | |
raise # noqa: WPS532 |
tests/test_checker/test_noqa.py
Outdated
@@ -441,7 +442,7 @@ def test_noqa_fixture(absolute_path): | |||
) | |||
stdout, _ = process.communicate() | |||
|
|||
assert stdout.count('WPS') == 0 | |||
assert stdout.count('WPS') == 1 |
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.
assert stdout.count('WPS') == 1 | |
assert stdout.count('WPS') == 0 |
|
||
def test_raise_except( | ||
assert_errors, | ||
parse_ast_tree, | ||
default_options, | ||
): | ||
"""Testing that bare `raise` is not allowed without an except block.""" | ||
code = """ | ||
def hello(): | ||
try: | ||
x = 1 | ||
except Exception: | ||
raise ValueError('1') | ||
""" |
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 parametrize this test to test at least 2 cases:
raise SomeException()
insideexcept
block- bare
raise
insideexcept
block
def test_raise_except( | |
assert_errors, | |
parse_ast_tree, | |
default_options, | |
): | |
"""Testing that bare `raise` is not allowed without an except block.""" | |
code = """ | |
def hello(): | |
try: | |
x = 1 | |
except Exception: | |
raise ValueError('1') | |
""" | |
@pytest.mark.parametrize('raise_statement', [ | |
"raise ValueError('1')", | |
'raise', | |
]) | |
def test_raise_inside_except( | |
assert_errors, | |
parse_ast_tree, | |
default_options, | |
raise_statement, | |
): | |
"""Ensure ``raise`` statement inside ``except`` block allowed.""" | |
code = """ | |
try: | |
x = 1 | |
except Exception: | |
{raise_statement} | |
""".format(raise_statement=raise_statement) |
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.
You can also create one parametrized test for correct cases like suggested by Nikita in #1998 (comment) or me in #1998 (comment)
assert_errors(visitor, []) | ||
|
||
|
||
def test_raise_no_except( |
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.
def test_raise_no_except( | |
def test_raise_exception_no_except( |
parse_ast_tree, | ||
default_options, | ||
): | ||
"""Testing that bare `raise` is not allowed without an except block.""" |
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.
"""Testing that bare `raise` is not allowed without an except block.""" | |
"""Testing that `raise SomeException()` outside ``except`` block is allowed.""" |
Codecov Report
@@ Coverage Diff @@
## master #1998 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 118 118
Lines 6208 6218 +10
Branches 1382 1385 +3
=========================================
+ Hits 6208 6218 +10
Continue to review full report at Codecov.
|
I parameterized the test functions and now have 1 testcase that is correct and 1 testcase that raises an error in the linter. |
""" | ||
"""Testing correct instances of `raise SomeException()` and bare `raise`.""" | ||
code = None | ||
if bare_option: |
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.
personally, I dislike using if
statements in tests, because they make tests harder to read and maintain, so I am suggesting to parameterized function with ready to parse code strings, e.g.:
@pytest.mark.parametrize('code', [
raise_exception_raw.format('ValueError(1)'),
raise_exception_with_except_raw.format('ValueError(1)'),
raise_exception_with_except_raw.format(''),
])
def test_correct raise_statement(
assert_errors,
parse_ast_tree,
default_options,
code,
):
...
raise_exception_with_except = """ | ||
def check_exception_without_call(): | ||
try: | ||
x = 1 | ||
except Exception: | ||
raise {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.
NIP: it's not necessary to wrap try except
in function definition:
raise_exception_with_except = """ | |
def check_exception_without_call(): | |
try: | |
x = 1 | |
except Exception: | |
raise {0} | |
raise_exception_with_except_raw = """ | |
try: | |
x = 1 | |
except Exception: | |
raise {0} |
Implemented as |
I have made things!
Checklist
CHANGELOG.md
Related issues
🙏 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.