Iterating through a fixed Tuple of strings ("foo", "bar") makes the loop variable a str instead of Union[Literal["foo"], Literal["bar"]]. This makes it difficult to loop through indices of a TypedDict.´
https://mypy-play.net/?mypy=latest&python=3.8&gist=17fe6a875f727a01fe3a5c6dca13dba2
from typing import TypedDict
class FooDict(TypedDict):
foo: int
bar: int
foo = FooDict(foo=3, bar=3)
print(foo["foo"]) # Works
print(foo["bar"]) # Works
reveal_type(("foo", "bar")) # Revealed type is 'Tuple[Literal['foo']?, Literal['bar']?]'
for key in ("foo", "bar"):
reveal_type(key) # Revealed type is 'builtins.str'
print(foo[key]) # TypedDict key must be a string literal; expected one of ('foo', 'bar')
Iterating through a fixed
Tupleof strings("foo", "bar")makes the loop variable astrinstead ofUnion[Literal["foo"], Literal["bar"]]. This makes it difficult to loop through indices of aTypedDict.´https://mypy-play.net/?mypy=latest&python=3.8&gist=17fe6a875f727a01fe3a5c6dca13dba2