Skip to content

Commit 8d8d37b

Browse files
Hotfix issue 153 (#161)
* add failing test * minimal patch to fix test
1 parent eb85c48 commit 8d8d37b

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

bugbear.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,10 @@ def _to_name_str(node):
124124
# "pkg.mod.error", handling any depth of attribute accesses.
125125
if isinstance(node, ast.Name):
126126
return node.id
127-
assert isinstance(node, ast.Attribute)
128-
return _to_name_str(node.value) + "." + node.attr
127+
try:
128+
return _to_name_str(node.value) + "." + node.attr
129+
except AttributeError:
130+
return _to_name_str(node.value)
129131

130132

131133
def _typesafe_issubclass(cls, class_or_tuple):

tests/test_bugbear.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,20 @@ def test_does_not_crash_on_site_code(self):
340340
if f.endswith(".py"):
341341
BugBearChecker(filename=str(Path(dirname) / f))
342342

343+
def test_does_not_crash_on_tuple_expansion_in_except_statement(self):
344+
# akin to test_does_not_crash_on_any_valid_code
345+
# but targets a rare case that's not covered by hypothesmith.from_grammar
346+
# see https://github.com/PyCQA/flake8-bugbear/issues/153
347+
syntax_tree = ast.parse(
348+
"grey_list = (ValueError,)\n"
349+
"black_list = (TypeError,)\n"
350+
"try:\n"
351+
" int('1e3')\n"
352+
"except (*grey_list, *black_list):\n"
353+
" print('error caught')"
354+
)
355+
BugBearVisitor(filename="<string>", lines=[]).visit(syntax_tree)
356+
343357

344358
if __name__ == "__main__":
345359
unittest.main()

0 commit comments

Comments
 (0)