From c973bc2dd9d20ea2b75f374e22fa619cdac4b92b Mon Sep 17 00:00:00 2001 From: Cooper Lees Date: Thu, 1 Apr 2021 12:46:52 -0700 Subject: [PATCH 1/2] Add another hasattr() check to b017 visit - Ensure we ignore nodes with no func --- bugbear.py | 3 ++- tests/b017.py | 8 ++++++++ tests/test_bugbear.py | 2 +- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/bugbear.py b/bugbear.py index c4f09d8..fc4b71a 100644 --- a/bugbear.py +++ b/bugbear.py @@ -439,7 +439,8 @@ def check_for_b017(self, node): item = node.items[0] item_context = item.context_expr if ( - hasattr(item_context.func, "attr") + hasattr(item_context, "func") + and hasattr(item_context.func, "attr") # noqa W503 and item_context.func.attr == "assertRaises" # noqa W503 and len(item_context.args) == 1 # noqa W503 and item_context.args[0].id == "Exception" # noqa W503 diff --git a/tests/b017.py b/tests/b017.py index 13a4c06..69b43fc 100644 --- a/tests/b017.py +++ b/tests/b017.py @@ -5,6 +5,14 @@ import unittest +CONSTANT = True + + +def something_else() -> None: + for i in (1, 2, 3): + print(i) + + class Foobar(unittest.TestCase): def evil_raises(self) -> None: with self.assertRaises(Exception): diff --git a/tests/test_bugbear.py b/tests/test_bugbear.py index a797a33..8bd279f 100644 --- a/tests/test_bugbear.py +++ b/tests/test_bugbear.py @@ -210,7 +210,7 @@ def test_b017(self): filename = Path(__file__).absolute().parent / "b017.py" bbc = BugBearChecker(filename=str(filename)) errors = list(bbc.run()) - expected = self.errors(B017(10, 8)) + expected = self.errors(B017(18, 8)) self.assertEqual(errors, expected) def test_b301_b302_b305(self): From 78c2729532887a6b566d64b806d3e9358aa98718 Mon Sep 17 00:00:00 2001 From: Cooper Lees Date: Thu, 1 Apr 2021 12:49:49 -0700 Subject: [PATCH 2/2] Blacken b017.py --- tests/b017.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/b017.py b/tests/b017.py index 69b43fc..e9b7430 100644 --- a/tests/b017.py +++ b/tests/b017.py @@ -10,7 +10,7 @@ def something_else() -> None: for i in (1, 2, 3): - print(i) + print(i) class Foobar(unittest.TestCase):