Skip to content

Commit

Permalink
Fix crash on raise with non-Name value
Browse files Browse the repository at this point in the history
Fixes #449
  • Loading branch information
JelleZijlstra committed Jan 16, 2024
1 parent 9fe5ca3 commit 4ecd672
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 1 deletion.
6 changes: 6 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
~~~~~~~

Expand Down
4 changes: 3 additions & 1 deletion bugbear.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions tests/b036.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
1 change: 1 addition & 0 deletions tests/test_bugbear.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 4ecd672

Please sign in to comment.