Problems with `get` on a TypedDict
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Bug Report
This bug is a bit hard to describe, so you should take a look at the test code. Feel free to edit the title.
As you can see in the test code, a expects dict[Literal["exclude"], list[str]]. test_dict, which is passed to b has a opional key named key with this type. As this type is optional, we use get here. Get returns {"exclude": []}) in case key does not exists in test_dict. {"exclude": []}) fits the type dict[Literal["exclude"], list[str]] but Mypy think it does not fit.
This bug does not exists in Mypy 1.17.
To Reproduce
https://mypy-play.net/?mypy=1.18.2&python=3.12&gist=932063170e6ad2c3ab798153870b82d4
Expected Behavior
Mypy should not find any errors
Actual Behavior
main.py:13: error: Argument 1 to "a" has incompatible type "dict[Literal['exclude'], list[str]] | dict[str, list[Never]]"; expected "dict[Literal['exclude'], list[str]]" [arg-type]
Found 1 error in 1 file (checked 1 source file)
Your Environment
- Mypy version used: 1.18.2
- Mypy command-line flags: None
- Mypy configuration options from
mypy.ini(and other config files): None - Python version used: 3.13.7
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the linked mypy-play reproduction in main.py, running it against mypy 1.17 and 1.18.2 to isolate the regression. Trace how TypedDict optional-key get expressions infer their fallback type; done means the reproduction produces no errors while preserving correct type checking.
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
- 30/100