Unexpected Overloading overlap if String Literal is used within Sequence/Iterable/Collection
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
Bug Report
If I have a function that accepts a String Literal and Sequence of String Literal and want to overload this function for these two cases, mypy always returns:
Overloaded function signatures 1 and 2 overlap with incompatible return types [misc]
from typing import overload, Literal
@overload
def over(bar: Literal["bar"]) -> int: # Overloaded function signatures 1 and 2 overlap with incompatible return types [misc]
...
@overload
def over(bar: Sequence[Literal["foo"]]) -> str:
...
def over(bar: Literal["bar"]|Sequence[Literal["foo"]]) -> str|int:
if bar == "bar":
return 0
return ", ".join(bar)
To Reproduce
https://mypy-play.net/?mypy=latest&python=3.11&gist=d28d528c0765dc0a4a004c360a4008a4
Expected Behavior
No error, as if we would use a list instead:
@overload
def load(bar: Literal["bar"]) -> int:
...
@overload
def load(bar: list[Literal["foo"]]) -> str:
...
def load(bar: Literal["bar"]|list[Literal["foo"]]) -> str|int:
if bar == "bar":
return 0
return ", ".join(bar)
Actual Behavior
Mypy returns:
Overloaded function signatures 1 and 2 overlap with incompatible return types [misc]
Your Environment
MyPy playground....
- Mypy version used: 1.20
- Mypy command-line flags: none
- Mypy configuration options from
mypy.ini(and other config files): none - Python version used: tested with 3.10 and 3.11
Note
I am aware that a str is also a Sequence[str]. But as we using literals here, this shouldn't matter.
This is only an issue for mypy if used with overloads, it works correctly if simply used within a function
# Simple example, that if we use Sequences and Literals, mypy
# can determine stuff correctly
def foo(bar: Literal["bar"]|Sequence[Literal["foo"]]) -> None:
return None
foo("bar") # okay
foo(["foo"]) # okay
foo("foo") # not okay - as expected
foo(["bar"]) # not okay - as expected
Note that pyright does detect the overloads correctly (kind of).
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 eseguendo la riproduzione collegata di mypy-play e confronta gli esempi di overload che usano Sequence, Iterable, Collection e list con i controlli finali delle chiamate di funzione. Traccia la compatibilità degli overload e la gestione di Literal coinvolte nella diagnostica segnalata. Il lavoro è completato quando gli overload validi di String Literal e di sequenza non producono più un errore di sovrapposizione incompatibile, mentre le chiamate non valide continuano a essere rifiutate.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- compilers, devtools
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100