-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Closed
Description
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')
qdbp, DriesSchaumont, eigenein, eddcorts, austinbutler and 8 more