Unions of Literals are not accepted as TypedDict Keys
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 35/100
Direzione di ricerca
Inizia con le riproduzioni minimal_fail e check_ranges nell'esempio Mypy Play collegato, quindi confrontale con minimal_okay. La modifica è completa quando le unioni di tipi literal vengono accettate come chiavi TypedDict senza l'errore literal-required, incluso il caso di unione annidata.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Bug Report
If Literals are given as Unions they aren't accepted as TypedDict Keys
To Reproduce
from typing import TypedDict, reveal_type, Final, Literal, TypeAlias, get_args
from datetime import datetime
class RangeInfo(TypedDict):
begin: datetime
end: datetime
created_start: datetime
created_end: datetime
# …
TimeRange: TypeAlias = Literal["begin", "end"]
CreatedRange: TypeAlias = Literal["created_start", "created_end"]
RANGES: Final[tuple[tuple[TimeRange | CreatedRange, ...],... ]] = (
get_args(TimeRange),
get_args(CreatedRange),
# … much more range names
)
def check_ranges(option: RangeInfo) -> None:
"""Ensure given datetime ranges are valid"""
for range_tuple in RANGES:
reveal_type(range_tuple) # builtins.tuple[Union[Union[Literal['begin'], Literal['end']], Union[Literal['created_start'], Literal['created_end']]], ...]
for range_val in range_tuple:
reveal_type(range_val) # Union[Union[Literal['begin'], Literal['end']], Union[Literal['created_start'], Literal['created_end']]]
if not isinstance(option[range_val], datetime): # ❌ TypedDict key must be a string literal; expected one of ("begin", "end", "created_start", "created_end") [literal-required]
raise ValueError(f"{range_val} is not a datetime")
# … much more checks
def minimal_okay(union: Literal["begin"] | Literal["end"], option: RangeInfo) -> datetime:
return option[union]
def minimal_fail(union: TimeRange | CreatedRange, option: RangeInfo) -> datetime:
return option[union] # ❌ TypedDict key must be a string literal; expected one of ("begin", "end", "created_start", "created_end") [literal-required]
Expected Behavior
There should be no type error, Literals in Unions should be flattened.
I would love if unions of literals are flattened in general.
Literal["a"] | Literal["b"] | (Literal["c"] | Literal["d"]) == Literal["a", "b", "c", "d"]
But I guess this is yet another issue.
Actual Behavior
Can't access typed dict without type error
Your Environment
(see mypy play)
Related #16813
- Lingua principale
- Python
- Stelle
- 20.6k
- Fork
- 3.3k
- Merge medio
- 1g 18h
- PR unite (30g)
- 54
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di python/mypy
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
-
documentation
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
-
bug topic-configuration topic-error-reporting
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
Issue simili
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 86/100
zostera/django-bootstrap4#894 ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
use-agent-os/agent-os#3276 ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100
zephyrproject-rtos/zephyr#119726 ·
-
area/auth bug comp/agent P3 platform/discord type/security
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100
NousResearch/hermes-agent#117848 ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
zilliztech/memsearch#759 ·