Skip to content

Commit a97d3db

Browse files
authored
Add another hasattr() check to b017 visit (#165)
* Add another hasattr() check to b017 visit - Ensure we ignore nodes with no func * Blacken b017.py
1 parent bfd3b7e commit a97d3db

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

bugbear.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,8 @@ def check_for_b017(self, node):
439439
item = node.items[0]
440440
item_context = item.context_expr
441441
if (
442-
hasattr(item_context.func, "attr")
442+
hasattr(item_context, "func")
443+
and hasattr(item_context.func, "attr") # noqa W503
443444
and item_context.func.attr == "assertRaises" # noqa W503
444445
and len(item_context.args) == 1 # noqa W503
445446
and item_context.args[0].id == "Exception" # noqa W503

tests/b017.py

+8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@
55
import unittest
66

77

8+
CONSTANT = True
9+
10+
11+
def something_else() -> None:
12+
for i in (1, 2, 3):
13+
print(i)
14+
15+
816
class Foobar(unittest.TestCase):
917
def evil_raises(self) -> None:
1018
with self.assertRaises(Exception):

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(10, 8))
213+
expected = self.errors(B017(18, 8))
214214
self.assertEqual(errors, expected)
215215

216216
def test_b301_b302_b305(self):

0 commit comments

Comments
 (0)