Skip to content

Commit

Permalink
Add another hasattr() check to b017 visit (#165)
Browse files Browse the repository at this point in the history
* Add another hasattr() check to b017 visit
- Ensure we ignore nodes with no func

* Blacken b017.py
  • Loading branch information
cooperlees committed Apr 1, 2021
1 parent bfd3b7e commit a97d3db
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
3 changes: 2 additions & 1 deletion bugbear.py
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions tests/b017.py
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_bugbear.py
Expand Up @@ -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):
Expand Down

0 comments on commit a97d3db

Please sign in to comment.