[flake8-bugbear] Fix B017 false negatives for keyword exception arguments (#19217)

Co-authored-by: Micha Reiser <micha@reiser.io>
This commit is contained in:
Dan Parizher 2025-07-11 12:43:09 -04:00 committed by GitHub
parent 30ee44770d
commit 110765154f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 74 additions and 3 deletions

View file

@ -1,6 +1,6 @@
"""
Should emit:
B017 - on lines 24, 28, 46, 49, 52, and 58
B017 - on lines 24, 28, 46, 49, 52, 58, 62, 68, and 71
"""
import asyncio
import unittest
@ -56,3 +56,17 @@ def test_pytest_raises():
with contextlib.nullcontext(), pytest.raises(Exception):
raise ValueError("Multiple context managers")
def test_pytest_raises_keyword():
with pytest.raises(expected_exception=Exception):
raise ValueError("Should be flagged")
def test_assert_raises_keyword():
class TestKwargs(unittest.TestCase):
def test_method(self):
with self.assertRaises(exception=Exception):
raise ValueError("Should be flagged")
with self.assertRaises(exception=BaseException):
raise ValueError("Should be flagged")

View file

@ -87,9 +87,14 @@ fn detect_blind_exception(
}
}
let first_arg = arguments.args.first()?;
let exception_argument_name = if is_pytest_raises {
"expected_exception"
} else {
"exception"
};
let builtin_symbol = semantic.resolve_builtin_symbol(first_arg)?;
let exception_expr = arguments.find_argument_value(exception_argument_name, 0)?;
let builtin_symbol = semantic.resolve_builtin_symbol(exception_expr)?;
match builtin_symbol {
"Exception" => Some(ExceptionKind::Exception),

View file

@ -43,3 +43,29 @@ B017_0.py:57:36: B017 Do not assert blind exception: `Exception`
| ^^^^^^^^^^^^^^^^^^^^^^^^ B017
58 | raise ValueError("Multiple context managers")
|
B017_0.py:62:10: B017 Do not assert blind exception: `Exception`
|
61 | def test_pytest_raises_keyword():
62 | with pytest.raises(expected_exception=Exception):
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ B017
63 | raise ValueError("Should be flagged")
|
B017_0.py:68:18: B017 Do not assert blind exception: `Exception`
|
66 | class TestKwargs(unittest.TestCase):
67 | def test_method(self):
68 | with self.assertRaises(exception=Exception):
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ B017
69 | raise ValueError("Should be flagged")
|
B017_0.py:71:18: B017 Do not assert blind exception: `BaseException`
|
69 | raise ValueError("Should be flagged")
70 |
71 | with self.assertRaises(exception=BaseException):
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ B017
72 | raise ValueError("Should be flagged")
|

View file

@ -43,3 +43,29 @@ B017_0.py:57:36: B017 Do not assert blind exception: `Exception`
| ^^^^^^^^^^^^^^^^^^^^^^^^ B017
58 | raise ValueError("Multiple context managers")
|
B017_0.py:62:10: B017 Do not assert blind exception: `Exception`
|
61 | def test_pytest_raises_keyword():
62 | with pytest.raises(expected_exception=Exception):
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ B017
63 | raise ValueError("Should be flagged")
|
B017_0.py:68:18: B017 Do not assert blind exception: `Exception`
|
66 | class TestKwargs(unittest.TestCase):
67 | def test_method(self):
68 | with self.assertRaises(exception=Exception):
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ B017
69 | raise ValueError("Should be flagged")
|
B017_0.py:71:18: B017 Do not assert blind exception: `BaseException`
|
69 | raise ValueError("Should be flagged")
70 |
71 | with self.assertRaises(exception=BaseException):
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ B017
72 | raise ValueError("Should be flagged")
|