Skip to content

Commit 4364218

Browse files
authored
[fix] Fix crash when comparing invalid dict literal (#2796)
1 parent 8caf666 commit 4364218

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

ChangeLog

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ What's New in astroid 4.0.0?
77
============================
88
Release date: TBA
99

10+
* Fix crash when comparing invalid dict literal
11+
12+
Closes #2522
13+
1014
* Removed internal functions ``infer_numpy_member``, ``name_looks_like_numpy_member``, and
1115
``attribute_looks_like_numpy_member`` from ``astroid.brain.brain_numpy_utils``.
1216

astroid/nodes/node_classes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1851,7 +1851,7 @@ def last_child(self):
18511851
# TODO: move to util?
18521852
@staticmethod
18531853
def _to_literal(node: SuccessfulInferenceResult) -> Any:
1854-
# Can raise SyntaxError or ValueError from ast.literal_eval
1854+
# Can raise SyntaxError, ValueError, or TypeError from ast.literal_eval
18551855
# Can raise AttributeError from node.as_string() as not all nodes have a visitor
18561856
# Is this the stupidest idea or the simplest idea?
18571857
return ast.literal_eval(node.as_string())
@@ -1887,7 +1887,7 @@ def _do_compare(
18871887

18881888
try:
18891889
left, right = self._to_literal(left), self._to_literal(right)
1890-
except (SyntaxError, ValueError, AttributeError):
1890+
except (SyntaxError, ValueError, AttributeError, TypeError):
18911891
return util.Uninferable
18921892

18931893
try:

tests/test_regrtest.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,3 +543,10 @@ class a: ...
543543
)
544544
assert isinstance(node, nodes.ClassDef)
545545
assert node.name == "a"
546+
547+
548+
def test_regression_infer_dict_literal_comparison_uninferable() -> None:
549+
"""Regression test for issue #2522."""
550+
node = extract_node("{{}}>0")
551+
inferred = next(node.infer())
552+
assert inferred.value == Uninferable

0 commit comments

Comments
 (0)