Incompatible override when overriding non-overload with overload
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
from typing import overload, Union
class A:
def f(self, x: Union[int, str]) -> None: ...
class B(A):
@overload
def f(self, x: int) -> None: ...
@overload
def f(self, x: str) -> None: ...
def f(self, x: Union[int, str]) -> None: ...
https://mypy-play.net/?mypy=latest&python=3.10&gist=37c2291afd7ddd170576b1de208ba77c
produces:
main.py:7: error: Signature of "f" incompatible with supertype "A"
main.py:7: note: Superclass:
main.py:7: note: def f(self, x: Union[int, str]) -> None
main.py:7: note: Subclass:
main.py:7: note: @overload
main.py:7: note: def f(self, x: int) -> None
main.py:7: note: @overload
main.py:7: note: def f(self, x: str) -> None
Found 1 error in 1 file (checked 1 source file)
But as far as I can see this is safe. Mypy accepts a union as an argument to B.f:
arg: Union[int, str]
B().f(arg)
Of course, the overloads are useless in this toy example, but the error also comes up if you vary the return type in the overloads. This came up in python/typeshed#7510.
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 Beispiel im verlinkten mypy-play-Fall zu reproduzieren, und vergleiche die Diagnosen für A.f und B.f. Verfolge die Prüfung der Override-Kompatibilität für eine überladene Methode der Unterklasse gegenüber einer nicht überladenen Methode der Oberklasse. Als erledigt gilt, dass dieses sichere Override akzeptiert wird, während inkompatible Überladungen weiterhin abgelehnt werden.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- compilers
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100