Skip to content

Commit 3cab68c

Browse files
authored
Bugfix: Verify the element in item_context.args is of type ast.Name (#166)
* Add B017 - detection for a bad form of assertRaises ```with assertRaises(Exception):``` is basically a "catch 'em all" assert that casts far too broad of a net when it comes to detecting failures in code being tested. Assertions should be testing specific failure cases, not "did Python throw /any/ type of error?", and so the context manager form, or the assertRaisesRegex form are far better to use. * Amend documentation, revert version change * Bugfix: Add more conditionals to B017 checks * Bugfix: Add more conditionals to B017 checks Attempts to ensure we're only looking at Name type objects in the item_context.args, since apparently this code is still matching other objects that don't have an id attr. * More updates to fix the B017 breakage * Black things * And change the line number again due to black
1 parent d3bf9ab commit 3cab68c

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

bugbear.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@
1010
from keyword import iskeyword
1111

1212
import attr
13+
1314
import pycodestyle
1415

15-
__version__ = "21.4.2"
16+
__version__ = "21.4.3"
1617

1718
LOG = logging.getLogger("flake8.bugbear")
1819

@@ -443,6 +444,7 @@ def check_for_b017(self, node):
443444
and hasattr(item_context.func, "attr") # noqa W503
444445
and item_context.func.attr == "assertRaises" # noqa W503
445446
and len(item_context.args) == 1 # noqa W503
447+
and isinstance(item_context.args[0], ast.Name) # noqa W503
446448
and item_context.args[0].id == "Exception" # noqa W503
447449
and not item.optional_vars # noqa W503
448450
):

tests/b017.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
"""
22
Should emit:
3-
B017 - on lines 10
3+
B017 - on lines 20
44
"""
55
import unittest
6-
6+
import asyncio
77

88
CONSTANT = True
99

@@ -13,6 +13,10 @@ def something_else() -> None:
1313
print(i)
1414

1515

16+
class Foo:
17+
pass
18+
19+
1620
class Foobar(unittest.TestCase):
1721
def evil_raises(self) -> None:
1822
with self.assertRaises(Exception):
@@ -26,3 +30,7 @@ def context_manager_raises(self) -> None:
2630
def regex_raises(self) -> None:
2731
with self.assertRaisesRegex(Exception, "Regex is good"):
2832
raise Exception("Regex is good")
33+
34+
def raises_with_absolute_reference(self):
35+
with self.assertRaises(asyncio.CancelledError):
36+
Foo()

tests/test_bugbear.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ def test_b017(self):
210210
filename = Path(__file__).absolute().parent / "b017.py"
211211
bbc = BugBearChecker(filename=str(filename))
212212
errors = list(bbc.run())
213-
expected = self.errors(B017(18, 8))
213+
expected = self.errors(B017(22, 8))
214214
self.assertEqual(errors, expected)
215215

216216
def test_b301_b302_b305(self):

0 commit comments

Comments
 (0)