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

Fix crash on raise with non-Name value #450

Merged
merged 1 commit into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading