Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix: Verify the element in item_context.args is of type ast.Name #166

Merged
merged 12 commits into from Apr 1, 2021
Merged
4 changes: 3 additions & 1 deletion bugbear.py
Expand Up @@ -10,9 +10,10 @@
from keyword import iskeyword

import attr

import pycodestyle

__version__ = "21.4.2"
__version__ = "21.4.3"

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

Expand Down Expand Up @@ -443,6 +444,7 @@ def check_for_b017(self, node):
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 isinstance(item_context.args[0], ast.Name) # noqa W503
and item_context.args[0].id == "Exception" # noqa W503
and not item.optional_vars # noqa W503
):
Expand Down
12 changes: 10 additions & 2 deletions tests/b017.py
@@ -1,9 +1,9 @@
"""
Should emit:
B017 - on lines 10
B017 - on lines 20
"""
import unittest

import asyncio

CONSTANT = True

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


class Foo:
pass


class Foobar(unittest.TestCase):
def evil_raises(self) -> None:
with self.assertRaises(Exception):
Expand All @@ -26,3 +30,7 @@ def context_manager_raises(self) -> None:
def regex_raises(self) -> None:
with self.assertRaisesRegex(Exception, "Regex is good"):
raise Exception("Regex is good")

def raises_with_absolute_reference(self):
with self.assertRaises(asyncio.CancelledError):
Foo()
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(18, 8))
expected = self.errors(B017(22, 8))
self.assertEqual(errors, expected)

def test_b301_b302_b305(self):
Expand Down