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
{{ message }}
Repository navigation
TypedDict fields cannot be defined with variables #8186
Using the alternative definition syntax for TypedDict (which should be equivalent according to PEP 589), the field names cannot be stored in variables (expected). But, I would expect Final or Literal typed variables to be accepted. See the following example:
$ mypy repro.py
repro.py:8: error: Invalid TypedDict() field name
repro.py:9: error: Extra key 'x'for TypedDict "TypedDict"
repro.py:12: error: Invalid TypedDict() field name
repro.py:13: error: Extra key 'x'for TypedDict "TypedDict"
Found 4 errors in 1 file (checked 1 source file)
What is the behaviour/output you expect?
No errors, or at least one of the two options succeeding (Final or Literal).
The type annotations are equivalent, as far as I can tell.
We have defined constants for the field names of dictionaries read from JSON files. I would like to make TypedDict's for these various dictionaries so that mypy can better check our code (instead of marking all dicts with Dict[str, Any].
I would like to define the fields using these predefined constants.
Activity
ilevkivskyi commentedon Dec 20, 2019
This is essentially a duplicate of #4128