overload not working with Generic type
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
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?
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne damit, das bereitgestellte Python-Snippet mit mypy 0.770 auszuführen und die beiden reveal_type-Ergebnisse zu vergleichen, insbesondere die generische Player-Überladung. Verfolge das Verhalten von Überladung und Typinferenz, das am PlayerConcrete-Aufruf beteiligt ist; abgeschlossen ist es, wenn der nicht-optionale Aufruf PlayerConcrete statt Any meldet, während das bestehende optionale Verhalten korrekt bleibt.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- devtools
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Klar beschrieben
- Anfängerfreundlichkeit
- 45/100