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

Issue 1974 #1998

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open

Issue 1974 #1998

wants to merge 18 commits into from

Conversation

amansr02
Copy link

@amansr02 amansr02 commented Apr 24, 2021

I have made things!

Checklist

  • I have double checked that there are no unrelated changes in this pull request (old patches, accidental config files, etc)
  • I have created at least one test case for the changes I have made
  • I have updated the documentation for the changes I have made
  • I have added my changes to the 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.

Copy link
Collaborator

@skarzi skarzi left a 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 😄

wemake_python_styleguide/logic/tree/operators.py Outdated Show resolved Hide resolved
wemake_python_styleguide/violations/refactoring.py Outdated Show resolved Hide resolved
wemake_python_styleguide/violations/refactoring.py Outdated Show resolved Hide resolved
wemake_python_styleguide/violations/refactoring.py Outdated Show resolved Hide resolved
wemake_python_styleguide/visitors/ast/keywords.py Outdated Show resolved Hide resolved
tests/fixtures/noqa/noqa.py Outdated Show resolved Hide resolved
wemake_python_styleguide/visitors/ast/keywords.py Outdated Show resolved Hide resolved
tests/fixtures/noqa/noqa.py Show resolved Hide resolved
@amansr02
Copy link
Author

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?

Copy link
Collaborator

@skarzi skarzi left a 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!

tests/fixtures/noqa/noqa.py Outdated Show resolved Hide resolved
tests/test_visitors/test_ast/test_keywords/test_raise.py Outdated Show resolved Hide resolved
@@ -130,3 +131,22 @@ def test_bare_raise(
visitor.run()

assert_errors(visitor, [])


def test_bare_raise_no_except(
Copy link
Collaborator

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 inside except block
  • raise SomeException() inside except block
  • raise SomeException() not nested in except block

wemake_python_styleguide/logic/safe_eval.py Outdated Show resolved Hide resolved
wemake_python_styleguide/visitors/ast/keywords.py Outdated Show resolved Hide resolved
@amansr02
Copy link
Author

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.

Copy link
Member

@sobolevn sobolevn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot!

@@ -442,7 +442,7 @@ def test_noqa_fixture(absolute_path):
)
stdout, _ = process.communicate()

assert stdout.count('WPS') == 0
assert stdout.count('WPS') == 1
Copy link
Member

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 = """
Copy link
Member

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.

Copy link
Collaborator

@skarzi skarzi left a 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


def detect_bare_raise1():
"""Function to check if the bare raise is detected."""
raise
Copy link
Collaborator

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

Suggested change
raise
raise # noqa: WPS532

@@ -441,7 +442,7 @@ def test_noqa_fixture(absolute_path):
)
stdout, _ = process.communicate()

assert stdout.count('WPS') == 0
assert stdout.count('WPS') == 1
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert stdout.count('WPS') == 1
assert stdout.count('WPS') == 0

Comment on lines 135 to 148

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')
"""
Copy link
Collaborator

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() inside except block
  • bare raise inside except block
Suggested change
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)

Copy link
Collaborator

@skarzi skarzi Apr 26, 2021

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(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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."""
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"""Testing that bare `raise` is not allowed without an except block."""
"""Testing that `raise SomeException()` outside ``except`` block is allowed."""

@codecov
Copy link

codecov bot commented Apr 26, 2021

Codecov Report

Merging #1998 (fc3db38) into master (b659768) will not change coverage.
The diff coverage is 100.00%.

❗ Current head fc3db38 differs from pull request most recent head e759a2d. Consider uploading reports for the commit e759a2d to get more accurate results
Impacted file tree graph

@@            Coverage Diff            @@
##            master     #1998   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          118       118           
  Lines         6208      6218   +10     
  Branches      1382      1385    +3     
=========================================
+ Hits          6208      6218   +10     
Impacted Files Coverage Δ
wemake_python_styleguide/violations/refactoring.py 100.00% <100.00%> (ø)
wemake_python_styleguide/visitors/ast/keywords.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b659768...e759a2d. Read the comment docs.

@amansr02
Copy link
Author

I parameterized the test functions and now have 1 testcase that is correct and 1 testcase that raises an error in the linter.
Also updated the noqa.py. Thank you for the feedback guys! Really helping me become familiar with the codebase. Any other changes?

"""
"""Testing correct instances of `raise SomeException()` and bare `raise`."""
code = None
if bare_option:
Copy link
Collaborator

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,
 ):
     ...

Comment on lines +21 to +26
raise_exception_with_except = """
def check_exception_without_call():
try:
x = 1
except Exception:
raise {0}
Copy link
Collaborator

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:

Suggested change
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}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants