Skip to content

"TypeError: unhashable type: 'dict'" when comparing invalid dict literal #2522

@correctmost

Description

@correctmost

Steps to reproduce

The following code triggers a TypeError in astroid:

a = {{}} > 0

This bug was discovered by OSS-Fuzz:
https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=60641 (report not public yet)

Current behavior

Exception on node <AssignName.a l.1 at 0x7d1710ba9310> in file 'corpus/a.py'
Traceback (most recent call last):
  File "pylint/pylint/checkers/utils.py", line 1365, in safe_infer
    value = next(infer_gen)
            ^^^^^^^^^^^^^^^
  File "astroid/astroid/nodes/node_ng.py", line 168, in infer
    for i, result in enumerate(self._infer(context=context, **kwargs)):
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "astroid/astroid/nodes/node_classes.py", line 1926, in _infer
    retval = self._do_compare(lhs, op, rhs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "astroid/astroid/nodes/node_classes.py", line 1894, in _do_compare
    left, right = self._to_literal(left), self._to_literal(right)
                  ^^^^^^^^^^^^^^^^^^^^^^
  File "astroid/astroid/nodes/node_classes.py", line 1862, in _to_literal
    return ast.literal_eval(node.as_string())
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/ast.py", line 112, in literal_eval
    return _convert(node_or_string)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/ast.py", line 94, in _convert
    return set(map(_convert, node.elts))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: unhashable type: 'dict'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "pylint/pylint/utils/ast_walker.py", line 91, in walk
    callback(astroid)
  File "pylint/pylint/checkers/base/name_checker/checker.py", line 423, in visit_assignname
    inferred_assign_type = utils.safe_infer(assign_type.value)
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "pylint/pylint/checkers/utils.py", line 1369, in safe_infer
    raise AstroidError from e
astroid.exceptions.AstroidError

Expected behavior

No crash

Version

d8dbc46

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions