python / python/mypy

TypedDict does not treat union of string literals as a string literal

Open
#20,235 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-literal-types topic-typed-dict
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

To Reproduce

https://mypy-play.net/?mypy=latest&python=3.14&gist=a2d228294e0a7a613472376043c62642

from typing import NotRequired, TypedDict


class Foo(TypedDict):
    key1: NotRequired[str]
    key2: NotRequired[int]


foo = Foo()
for key in "key1", "key2":
    reveal_type(key)
    del foo[key]

Expected Behavior

This should type check with no errors.

Actual Behavior

main.py:11: note: Revealed type is "Literal['key1']? | Literal['key2']?"
main.py:12: error: Expected TypedDict key to be string literal  [misc]
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.18.2
  • Python version used: 3.14

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the example from the mypy-play link in main.py with mypy 1.18.2 and Python 3.14. Trace how the TypedDict deletion check handles the revealed union of string literals, then add coverage for this example and verify that it type checks without errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.