Code: Select all
from typing import Literal
SupportedKey = Literal["one", "two"]
d : dict[SupportedKey, int] = {"one": 1, "two": 2}
fehl
Code: Select all
Type annotation for d does not match type of assignment [annotation-type-mismatch]
Annotation: dict[Literal['one', 'two'], int]
Assignment: dict[str, Literal[1, 2]]
< /code>
Beachtenstr
[*] zugewiesen
Code: Select all
Literal[1, 2]
Ich habe eine explizitere Methode ausprobiert, wobei die Schlüssel mit entsprechenden Tippen konstruiert sind:
Code: Select all
from typing import Literal, Final
SupportedKey = Literal["one", "two"]
KEY_ONE: Final[SupportedKey] = "one"
KEY_TWO: Final[SupportedKey] = "two"
d : dict[SupportedKey, int] = {KEY_ONE: 1, KEY_TWO: 2}
< /code>
Dies schlägt jedoch ähnlich - die Schlüssel werden immer noch als allgemeiner als wörtlicher als wörtlich abgeleitet: < /p>
Type annotation for d does not match type of assignment [annotation-type-mismatch]
Annotation: dict[Literal['one', 'two'], int]
Assignment: dict[str, int]