python / python/mypy

Wrong overload resolution with alias to Any

Offen
#15,630 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

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

Beschreibung

Bug Report

Overload resolution appears to pick the first matching overload if the argument type is an alias to Any. It should consider all overloads. This works correctly when using Any directly (without an alias). This came up in https://github.com/python/typeshed/issues/10405.

To Reproduce

from typing import Any, Union, overload

@overload
def my_origin(x: type) -> type: ...

@overload
def my_origin(x: Any) -> Any: ...

def my_origin(x: Any) -> Any:
    return x

def call_any(spec: Any) -> bool:
    return my_origin(spec) is Union

TypeSpec = Any

def call_alias(spec: TypeSpec) -> bool:
    return my_origin(spec) is Union

https://mypy-play.net/?mypy=latest&python=3.11&flags=strict&gist=26e08b7414caad553cd0e9549928da11

Expected Behavior

Both call_any and call_alias should type check without errors, since they are the same thing.

Actual Behavior

call_alias produces an error in strict mode:

proj/overload.py:18: error: Non-overlapping identity check (left operand type: "type", right operand type: "<typing special form>")  [comparison-overlap]

Your Environment

  • Mypy version used: 1.4.1
  • Mypy command-line flags: --strict
  • Python version used: 3.10.6

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

Führe zunächst den Strict-Mode-Reproducer aus dem Issue aus und vergleiche die Überladungsauflösung für Any mit dem TypeSpec-Alias. Ermittle den Pfad der Überladungsauflösung, der für das unterschiedliche Ergebnis der Vergleichsüberlappung verantwortlich ist; abgeschlossen ist die Aufgabe, wenn sowohl call_any als auch call_alias fehlerfrei typgeprüft werden und ein Regressionstest den Fall abdeckt.

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

Neue Issues direkt in Ihr Postfach

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