(🐞) `overload`s that exhaust finite `Literal`s(`bool`/`Enum`) not treated as exhaustive
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 20.6k
- Fork
- 3.3k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
@overload
def foo(a: Literal[True]) -> int: ...
@overload
def foo(a: Literal[False]) -> str: ...
def foo(a: bool) -> object: ...
a: bool
reveal_type(foo(a)) # error: No overload variant of "foo" matches argument type "bool"
@overload
def foo(a: Literal[True]) -> int: ...
@overload
def foo(a: Literal[False]) -> str: ...
@overload
def foo(a: bool) -> object: ... # no error regarding impossible to match overload
a: bool
reveal_type(foo(a)) # object
@overload
def foo(a: Literal[True, False]) -> int | str: ...
@overload
def foo(a: bool) -> object: ... # error: Overloaded function signature 2 will never be matched: signature 1's parameter type(s) are the same or broader
def foo(a: bool) -> object: ...
a: bool
reveal_type(foo(a)) # int | str
Here mypy incorrectly forces us to implement a completely redundant overload for the non Literal case when it is already exhaustively covered by both literals. It is only when the literals are in the same overload (useless in practice, but just for demonstration) that mypy correctly handles this case.
Mypy should be doing 'union math' (or what ever it's called) to apply both literal overloads at once.
This also affects all other exhaustible Literals such as Enums.
This example is pulled directly from the docs, so I think they should be updated as well to an example that doesn't contain this confusing defect.
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.
Direzione di ricerca
Inizia riproducendo gli esempi di overload nell’issue e leggi la documentazione collegata su Literal types. Segui il matching degli overload e la gestione dei valori finiti di Literal ed Enum da parte di mypy. Il lavoro è completo quando overload Literal separati che coprono esaustivamente i casi non richiedono più un overload non-Literal ridondante e l’esempio di documentazione interessato è stato aggiornato.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- devtools
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 38/100