overload not working with Generic type
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
I'm running into an issue where a function overload is not working; I get no errors from mypy and one of the overload branches returns the proper type, but the other is returning Any instead of the type I would expect.
I've distilled it down to the snippet below. To reproduce, paste the following into a file and simply run mypy <filename>.
I'm running mypy version 0.770 on a Mac with homebrew Python 3.7.7
from typing import TypeVar, overload, Type, Optional, Generic
# Define a Player base class that can be paired with some data.
T = TypeVar('T')
class Player(Generic[T]):
pass
# Create an overloaded function to handle casting
# both Player and Optional[Player] to corresponding
# specific subtypes.
# (to ensure Optional state stays intact, unlike raw cast())
PT = TypeVar('PT', bound=Player)
@overload
def playercast(totype: Type[PT], obj: Player) -> PT:
...
@overload
def playercast(totype: Optional[Type[PT]],
obj: Optional[Player]) -> Optional[PT]:
...
def playercast(totype: Optional[Type[PT]],
obj: Optional[Player]) -> Optional[PT]:
return obj # type: ignore
# Now define a PlayerConcrete subtype, assign a few to Player vars,
# and then attempt to cast them back to PlayerConcrete.
class PlayerConcrete(Player[int]):
pass
player1: Optional[Player] = PlayerConcrete()
player2: Player = PlayerConcrete()
# Gives Optional[PlayerConcrete] as I would expect.
reveal_type(playercast(PlayerConcrete, player1))
# This, however, gives Any.
reveal_type(playercast(PlayerConcrete, player2))
# Doing the same thing with distinct functions works as expected.
def playercast_nonopt(totype: Type[PT], obj: Player) -> PT:
return obj # type: ignore
def playercast_opt(totype: Optional[Type[PT]],
obj: Optional[Player]) -> Optional[PT]:
return obj # type: ignore
# Gives Optional[PlayerConcrete].
reveal_type(playercast_opt(PlayerConcrete, player1))
# Gives PlayerConcrete.
reveal_type(playercast_nonopt(PlayerConcrete, player2))
Expected results:
- I would expect the second reveal_type() to return PT (ConcretePlayer in this case)
Actual results:
- The second reveal_type() returns Any
Should I expect this case to be functional? I've found that if Player is not a Generic class then it works. Is that a known limitation of overloads such as this, or is there a way to get this to work?
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 lo snippet Python fornito con mypy 0.770 e confronta i due risultati di reveal_type, in particolare l’overload generico di Player. Traccia il comportamento dell’overload e dell’inferenza dei tipi coinvolto nella chiamata a PlayerConcrete; il lavoro è completato quando la chiamata non opzionale restituisce PlayerConcrete anziché Any, mentre il comportamento opzionale esistente rimane corretto.
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
- Specificata chiaramente
- Idoneità per principianti
- 45/100