Skip to content

Crash for TypedDict with non-literal keys  #15557

@gsakkis

Description

@gsakkis

Crash Report

Creating a TypedDict with non-literal keys gives TypedDict() expects a dictionary literal as the second argument. Attempting to ignore this with # type: ignore causes an AssertionError if (and only if):

  • any error (unrelated to TypedDict) happens later in a different module that imports the one with the TypedDict definition
  • and .mypy_cache exists

Traceback

✗ mypy --show-traceback main.py
main.py:3: error: Unsupported operand types for + ("int" and "str")  [operator]
Found 1 error in 1 file (checked 1 source file)

✗ mypy --show-traceback main.py
Traceback (most recent call last):
  (...)
  File "mypy/main.py", line 95, in main
  File "mypy/main.py", line 174, in run_build
  File "mypy/build.py", line 197, in build
  File "mypy/build.py", line 270, in _build
  File "mypy/build.py", line 2927, in dispatch
  File "mypy/build.py", line 3318, in process_graph
  File "mypy/build.py", line 3399, in process_fresh_modules
  File "mypy/build.py", line 2114, in fix_cross_refs
  File "mypy/fixup.py", line 53, in fixup_module
  File "mypy/fixup.py", line 126, in visit_symbol_table
AssertionError: ('counts.Counts', 'counts.TypedDict')

To Reproduce

# counts.py

from typing import TypedDict

Counts = TypedDict("Counts", {k: int for k in "abc"})  # type: ignore
# main.py

from counts import Counts

print(1 + "a")

Your Environment

  • Mypy version used: 1.4.1
  • Python version used: 3.10

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions