python / python/mypy

Mypy seems to ignore explicitly given type parameter when a overload containing Never matches

Offen
#16,424 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug topic-overloads
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

Bug Report

When trying to create an object from a generic class and having overloads, one of which is parameterized with Never, initializing the class with the signature compatible with the overload for Never seems to result in a type parameterized by Never, regardless of the type being specified explicitly (k in the playground). Typing the variable directly seems to resolve this issue (w in the playground).
Pyright seems to give the same expected error in both instances.
I am sorry if this explanation is a bit long-winded, I don't really know how to explain it more succinctly.

To Reproduce

https://mypy-play.net/?mypy=master&python=3.12&gist=9ca64c2cde9f88a1000b10217cd5b2bd&flags=strict

Expected Behavior

Both the w and the k case from the playground should have the same error.

Actual Behavior

Only the w case actually gives an error, and the k case passes without issue, i.e. Test[int]() results in a variable of type Test[Never] for some reason, rather than mypy complaining that the signature is not right.

main.py:25: error: No overload variant of "Test" matches argument type "int"  [call-overload]
main.py:25: note: Possible overload variants:
main.py:25: note:     def __init__(self, t: None = ...) -> Test[NoReturn]
main.py:25: note:     def __init__(self, t: str) -> Test[str]
main.py:27: error: Incompatible types in assignment (expression has type "Test[NoReturn]", variable has type "Test[int]")  [assignment]
main.py:30: note: Revealed type is "__main__.Test[builtins.int]"
main.py:31: note: Revealed type is "__main__.Test[Never]"
main.py:32: note: Revealed type is "__main__.Test[Never]"
main.py:33: note: Revealed type is "__main__.Test[builtins.int]"
main.py:34: note: Revealed type is "Any"
main.py:36: note: Revealed type is "__main__.Test[builtins.int]"
main.py:37: note: Revealed type is "__main__.Test[Never]"
Found 2 errors in 1 file (checked 1 source file)

Your Environment

I don't believe any of this matters in this instance because the same issue happens on the playground as well.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit dem verlinkten mypy playground und reproduziere das Beispiel in main.py im strict mode mit Python 3.12. Verfolge, wie die Auflösung von Overloads mit dem explizit angegebenen Test[int] umgeht, wenn der mit Never parametrisierte overload übereinstimmt. Als erledigt gilt die Aufgabe, wenn die Fälle w und k denselben Fehler erzeugen, wie unter Expected Behavior beschrieben.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
tooling
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.