Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork3k
Open
Description
Bug Report
When dict unpacking is used to dynamically add optional keys to a TypedDict,mypy
raises atypeddict-item
error.
As dict unpacking is used to fill some keys from the TypedDict, my understanding is thatmypy
could validate the unpacked dictionaries considering all keys asNotRequired
, and only the final TypedDict respecting its formal definition.
To Reproduce
Gist:https://mypy-play.net/?mypy=latest&python=3.13&gist=b3377281f7f5a6f83dd9962d60302827
fromtypingimportNotRequired,TypedDictclassProject(TypedDict):name:strversion:NotRequired[str]defbuild_project(name:str,version:str|None=None)->Project:return {"name":name,**({"version":version}ifversionisnotNoneelse {}) }
Actual Behavior
$ mypy .main.py:12: error: Missing key "name" for TypedDict "Project" [typeddict-item]Found 1 error in 1 file (checked 1 source file)
Your Environment
- Mypy version used:
1.17.0
- Mypy command-line flags: N/A
- Mypy configuration options from
mypy.ini
(and other config files): N/A - Python version used:
3.13.5