mypy incorrectly reports incompatible return types for overloaded methods
Offen
Dieses Issue hat noch niemand übernommen.
bug
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
Bug Report
@overload
def foo(cls: type[dict], mapping: SupportsKeysAndGetItem[K, V], /) -> dict[K, V]: ...
@overload
def foo(cls: type[dict], mapping: SupportsKeysAndGetItem[str, V], /, **kwargs: V) -> dict[str, V]: ...
Return types are compatible.
class FrozendictBase(Generic[K, V]):
@overload
def __new__(
cls,
mapping: SupportsKeysAndGetItem[K, V],
/,
) -> 'FrozendictBase[K, V]': ...
@overload
def __new__(
cls,
mapping: SupportsKeysAndGetItem[str, V],
/,
**kwargs: V,
) -> 'FrozendictBase[str, V]': ...
Return types are also compatible, but mypy reports an error.
To Reproduce
- Download file.txt and save as
sandbox.py. - Run
mypy sandbox.py.
Expected Behavior
sandbox.py:8: error: Invariant type variable "V" used in protocol where covariant one is expected
This is due to #5775.
Actual Behavior
sandbox.py:8: error: Invariant type variable "V" used in protocol where covariant one is expected
sandbox.py:39: error: Overloaded function signatures 3 and 4 overlap with incompatible return types
sandbox.py:54: error: Overloaded function signatures 5 and 6 overlap with incompatible return types
Your Environment
- Mypy version used: mypy 0.950
- Mypy command-line flags:
- Mypy configuration options from
mypy.ini(and other config files): - Python version used: Python 3.10.4
- Operating system and version: Windows 10
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
Lade die verknüpfte Datei herunter und speichere sie als sandbox.py, führe dann mypy sandbox.py aus, um die Overload-Diagnosen zu reproduzieren. Untersuche die Meldungen zu inkompatiblen Rückgabetypen für die überladenen Signaturen 3/4 und 5/6; fertig ist es, wenn diese Meldungen nicht mehr erscheinen, während der erwartete Fehler zur invarianten Typvariable bestehen bleibt.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- compilers, devtools
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100