python / python/mypy

Overload resolution collapses to Any when Any is nested inside a generic argument, not just top-level

Aperta
#21,768 6 commenti 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia eseguendo lo snippet fornito con mypy 2.3.0 e leggendo la sezione della documentazione “Type checking calls to overloads”. Traccia la risoluzione degli overload per Sequence[Any] rispetto a Sequence[int]; il lavoro è completato quando il comportamento previsto è stabilito e il tipo rivelato, l’implementazione o la documentazione corrisponde a tale decisione.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
compilers
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Attiva
Chiarezza
Abbastanza chiara
Idoneità per principianti
55/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.