diff --git a/README.rst b/README.rst index 45acc66..524472f 100644 --- a/README.rst +++ b/README.rst @@ -349,6 +349,12 @@ MIT Change Log ---------- +Unreleased +~~~~~~~~~~ + +* B036: Fix crash on `raise` statements raising something other than + a bare name (#450) + 24.1.15 ~~~~~~~ diff --git a/bugbear.py b/bugbear.py index 988e995..8f47666 100644 --- a/bugbear.py +++ b/bugbear.py @@ -341,7 +341,9 @@ def visit_Raise(self, node: ast.Raise): """If we find a corresponding `raise` or `raise e` where e was from `except BaseException as e:` then we mark re_raised as True and can stop scanning.""" - if node.exc is None or node.exc.id == self.root.name: + if node.exc is None or ( + isinstance(node.exc, ast.Name) and node.exc.id == self.root.name + ): self._re_raised = True return return super().generic_visit(node) diff --git a/tests/b036.py b/tests/b036.py index 67f404d..933ee8f 100644 --- a/tests/b036.py +++ b/tests/b036.py @@ -52,3 +52,8 @@ pass except ValueError: raise # bad - raising within a nested try/except, but not within the main one + +try: + pass +except BaseException: + raise a.b from None # bad (regression test for #449) diff --git a/tests/test_bugbear.py b/tests/test_bugbear.py index 88e523c..0bac96b 100644 --- a/tests/test_bugbear.py +++ b/tests/test_bugbear.py @@ -618,6 +618,7 @@ def test_b036(self) -> None: B036(20, 0), B036(33, 0), B036(50, 0), + B036(58, 0), ) self.assertEqual(errors, expected)