Skip to content

Commit e2109ba

Browse files
Fridayai700claude
andauthored
Fix TypeError in is_literal_or_name on unhashable set literals (#339)
ast.literal_eval raises TypeError (not just SyntaxError/ValueError) when evaluating set literals containing unhashable types like dicts or lists. Catch TypeError as well to prevent the crash. Fixes #325 Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 79b86bd commit e2109ba

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

autoflake.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -787,7 +787,7 @@ def is_literal_or_name(value: str) -> bool:
787787
try:
788788
ast.literal_eval(value)
789789
return True
790-
except (SyntaxError, ValueError):
790+
except (SyntaxError, TypeError, ValueError):
791791
pass
792792

793793
if value.strip() in ["dict()", "list()", "set()"]:

test_autoflake.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1860,6 +1860,11 @@ def test_is_literal_or_name(self) -> None:
18601860
self.assertFalse(autoflake.is_literal_or_name("xyz.prop"))
18611861
self.assertFalse(autoflake.is_literal_or_name(" "))
18621862

1863+
def test_is_literal_or_name_with_unhashable_types(self) -> None:
1864+
"""Should not crash on set literals containing unhashable types."""
1865+
self.assertFalse(autoflake.is_literal_or_name("{{}}"))
1866+
self.assertFalse(autoflake.is_literal_or_name("{[]}"))
1867+
18631868
def test_is_python_file(self) -> None:
18641869
self.assertTrue(
18651870
autoflake.is_python_file(

0 commit comments

Comments
 (0)