-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[flake8-bugbear] Support non-context-manager calls in B017
#19063
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3bfae2f
fix-19050
danparizher f25dc89
remove comments from test
danparizher 7bbc953
implement feedback
danparizher d381956
add preview test
danparizher 3dc56dc
split tests
danparizher fe26add
avoid line number changes
ntBre ba492ef
whitespace
ntBre 0545a61
fix false positive
danparizher File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
4 changes: 2 additions & 2 deletions
4
...rces/test/fixtures/flake8_bugbear/B017.py → ...es/test/fixtures/flake8_bugbear/B017_0.py
This file contains hidden or 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
28 changes: 28 additions & 0 deletions
28
crates/ruff_linter/resources/test/fixtures/flake8_bugbear/B017_1.py
This file contains hidden or 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,28 @@ | ||
| """ | ||
| Should emit: | ||
| B017 - on lines 20, 21, 25, and 26 | ||
| """ | ||
| import unittest | ||
| import pytest | ||
|
|
||
|
|
||
| def something_else() -> None: | ||
| for i in (1, 2, 3): | ||
| print(i) | ||
|
|
||
|
|
||
| class Foo: | ||
| pass | ||
|
|
||
|
|
||
| class Foobar(unittest.TestCase): | ||
| def call_form_raises(self) -> None: | ||
| self.assertRaises(Exception, something_else) | ||
| self.assertRaises(BaseException, something_else) | ||
|
|
||
|
|
||
| def test_pytest_call_form() -> None: | ||
| pytest.raises(Exception, something_else) | ||
| pytest.raises(BaseException, something_else) | ||
|
|
||
| pytest.raises(Exception, something_else, match="hello") |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
4 changes: 4 additions & 0 deletions
4
...s/flake8_bugbear/snapshots/ruff_linter__rules__flake8_bugbear__tests__B017_B017_1.py.snap
This file contains hidden or 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,4 @@ | ||
| --- | ||
| source: crates/ruff_linter/src/rules/flake8_bugbear/mod.rs | ||
| --- | ||
|
|
45 changes: 45 additions & 0 deletions
45
...bugbear/snapshots/ruff_linter__rules__flake8_bugbear__tests__preview__B017_B017_0.py.snap
This file contains hidden or 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,45 @@ | ||
| --- | ||
| source: crates/ruff_linter/src/rules/flake8_bugbear/mod.rs | ||
| --- | ||
| B017_0.py:23:14: B017 Do not assert blind exception: `Exception` | ||
| | | ||
| 21 | class Foobar(unittest.TestCase): | ||
| 22 | def evil_raises(self) -> None: | ||
| 23 | with self.assertRaises(Exception): | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ B017 | ||
| 24 | raise Exception("Evil I say!") | ||
| | | ||
|
|
||
| B017_0.py:27:14: B017 Do not assert blind exception: `BaseException` | ||
| | | ||
| 26 | def also_evil_raises(self) -> None: | ||
| 27 | with self.assertRaises(BaseException): | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ B017 | ||
| 28 | raise Exception("Evil I say!") | ||
| | | ||
|
|
||
| B017_0.py:45:10: B017 Do not assert blind exception: `Exception` | ||
| | | ||
| 44 | def test_pytest_raises(): | ||
| 45 | with pytest.raises(Exception): | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^ B017 | ||
| 46 | raise ValueError("Hello") | ||
| | | ||
|
|
||
| B017_0.py:48:10: B017 Do not assert blind exception: `Exception` | ||
| | | ||
| 46 | raise ValueError("Hello") | ||
| 47 | | ||
| 48 | with pytest.raises(Exception), pytest.raises(ValueError): | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^ B017 | ||
| 49 | raise ValueError("Hello") | ||
| | | ||
|
|
||
| B017_0.py:57:36: B017 Do not assert blind exception: `Exception` | ||
| | | ||
| 55 | raise ValueError("This is also fine") | ||
| 56 | | ||
| 57 | with contextlib.nullcontext(), pytest.raises(Exception): | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^ B017 | ||
| 58 | raise ValueError("Multiple context managers") | ||
| | |
37 changes: 37 additions & 0 deletions
37
...bugbear/snapshots/ruff_linter__rules__flake8_bugbear__tests__preview__B017_B017_1.py.snap
This file contains hidden or 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,37 @@ | ||
| --- | ||
| source: crates/ruff_linter/src/rules/flake8_bugbear/mod.rs | ||
| --- | ||
| B017_1.py:20:9: B017 Do not assert blind exception: `Exception` | ||
| | | ||
| 18 | class Foobar(unittest.TestCase): | ||
| 19 | def call_form_raises(self) -> None: | ||
| 20 | self.assertRaises(Exception, something_else) | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ B017 | ||
| 21 | self.assertRaises(BaseException, something_else) | ||
| | | ||
|
|
||
| B017_1.py:21:9: B017 Do not assert blind exception: `BaseException` | ||
| | | ||
| 19 | def call_form_raises(self) -> None: | ||
| 20 | self.assertRaises(Exception, something_else) | ||
| 21 | self.assertRaises(BaseException, something_else) | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ B017 | ||
| | | ||
|
|
||
| B017_1.py:25:5: B017 Do not assert blind exception: `Exception` | ||
| | | ||
| 24 | def test_pytest_call_form() -> None: | ||
| 25 | pytest.raises(Exception, something_else) | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ B017 | ||
| 26 | pytest.raises(BaseException, something_else) | ||
| | | ||
|
|
||
| B017_1.py:26:5: B017 Do not assert blind exception: `BaseException` | ||
| | | ||
| 24 | def test_pytest_call_form() -> None: | ||
| 25 | pytest.raises(Exception, something_else) | ||
| 26 | pytest.raises(BaseException, something_else) | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ B017 | ||
| 27 | | ||
| 28 | pytest.raises(Exception, something_else, match="hello") | ||
| | |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.