You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Are you reporting a bug, or opening a feature request?
Feature request
Please insert below the code you are checking with mypy,
or a mock-up repro if the source is private. We would appreciate
if you try to simplify your case to a minimal repro.
Currently I can't insert items into TypedDict even after explicit check for a defined key:
from mypy_extensions import TypedDict
GroupDataDict = TypedDict(
'GroupDataDict', {"children": str,
"vars": str,
"hosts": str}, total=False
)
test_group: GroupDataDict = {}
key = "children"
if key in ("children", "vars"):
test_group[key] = "a"
if key not in ["children", "vars"]:
raise ValueError("")
test_group[key] = "b"
print(test_group)
What is the actual behavior/output?
-> % mypy typeddict_insertion.py
typeddict_insertion.py:12: error: TypedDict key must be a string literal; expected one of ('children', 'vars', 'hosts')
typeddict_insertion.py:16: error: TypedDict key must be a string literal; expected one of ('children', 'vars', 'hosts')
What is the behavior/output you expect?
No error
What are the versions of mypy and Python you are using?
mypy 0.620 + Python 3.7
Do you see the same issue after installing mypy from Git master?
I haven't tried
What are the mypy flags you are using? (For example --strict-optional)
Activity
ilevkivskyi commentedon Jul 15, 2018
This depends on literal types (already on our short-medium term roadmap, i.e. few months).
killthekitten commentedon Jul 9, 2020
It is kinda possible now with literal types:
It could be nicer, of course.