Overload resolution collapses to Any when Any is nested inside a generic argument, not just top-level
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
Bug Report
The docs state the return type becomes Any only when "an argument [is] of type Any". But when the Any is nested inside a generic type argument (Sequence[Any] vs Sequence[int]), mypy also collapses the result to Any, even though the first overload matches unambiguously. Is this intended? If so, the "Type checking calls to overloads" section should probably document the nested case.
To Reproduce
from collections.abc import Sequence
from typing import Any, overload, reveal_type
@overload
def f(s: Sequence[int]) -> str: ...
@overload
def f(s: object) -> int: ...
def f(s: object) -> int | str:
return "2"
concrete: Sequence[int] = [1]
nested_any: Sequence[Any] = [1]
reveal_type(f(concrete)) # Revealed type is "str" (expected)
reveal_type(f(nested_any)) # Revealed type is "Any" (surprising)
Expected Behavior
reveal_type on Sequence[Any] should show "str"
Actual Behavior
reveal_type on Sequence[Any] shows "Any"
Your Environment
- Mypy version used: mypy 2.3.0
- Mypy command-line flags: none
- Mypy configuration options from
mypy.ini(and other config files): none - Python version used: Python 3.14.6
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 Snippet mit mypy 2.3.0 auszuführen und den Dokumentationsabschnitt „Type checking calls to overloads“ zu lesen. Verfolge die Auflösung von Overloads für Sequence[Any] gegenüber Sequence[int]; fertig ist die Aufgabe, wenn das beabsichtigte Verhalten feststeht und der angezeigte Typ, die Implementierung oder die Dokumentation dieser Entscheidung entspricht.
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
- Aktiv
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 55/100