python / python/mypy

Mypy doesn't assign TypedDict output as Literals

Open
#16,534 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Bug Report

(A clear and concise description of what the bug is.)

To Reproduce

When I do this, I get an error saying a literal is required. When I add a literal it can't assign a string to a literal.

Actual Behavior

When using the first way, I got this error:

main.py:19: error: TypedDict key must be a string literal; expected one of ("b", "c", "d")  [literal-required]
Found 1 error in 1 file (checked 1 source file)

The second way this one:

main.py:19: error: Incompatible types in assignment (expression has type "str", variable has type "Literal['b', 'c', 'd']")  [assignment]
Found 1 error in 1 file (checked 1 source file)

Your Environment

I used the standard strict configuration from vscode, I don't know where I could find the version info and default command-line flags.

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 with the two linked mypy-play reproductions and compare their TypedDict and Literal behavior under the reported Python 3.11/latest setup. Done means both examples type-check as intended without the reported literal-required or incompatible-assignment 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
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.