TypeVar bound not respected in Generic class method overloads
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 20.6k
- Fork
- 3.3k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
Bug Report
Mypy chooses the wrong overload of a generic class method in the below code. str | Sequence[int] would match Sequence[ItemT] if ItemT was not bounded or constrained, but since it is constrained to non-str the overload for str | Sequence[ItemT] should be matched instead. The issue only affects generic class methods and not regular functions.
To Reproduce
from collections.abc import Sequence
from typing import Generic, TypeVar, overload
from typing_extensions import assert_type
T = TypeVar("T", covariant=True)
ItemT = TypeVar("ItemT", bound=int) # OR TypeVar("ItemT", int, float)
class MyGeneric(Generic[T]):
@overload
def transform(self: "MyGeneric[str]") -> "MyGeneric[str]": ...
@overload
def transform(self: "MyGeneric[Sequence[ItemT]]") -> "MyGeneric[Sequence[ItemT]]": ...
@overload
def transform(self: "MyGeneric[str | Sequence[ItemT]]") -> "MyGeneric[str | Sequence[ItemT]]": ...
def transform(self: "MyGeneric[str | Sequence[ItemT]]") -> "MyGeneric[str | Sequence[ItemT]]": ... # type: ignore
assert_type(MyGeneric[str]().transform(), MyGeneric[str])
assert_type(MyGeneric[Sequence[int]]().transform(), MyGeneric[Sequence[int]])
assert_type(MyGeneric[str | Sequence[int]]().transform(), MyGeneric[str | Sequence[int]])
# error: Expression is of type "MyGeneric[Sequence[object]]", not "MyGeneric[str | Sequence[int]]"
# ---
# The equivalent functions do not have the same issue
@overload
def transform(x: "MyGeneric[str]") -> "MyGeneric[str]": ...
@overload
def transform(x: "MyGeneric[Sequence[ItemT]]") -> "MyGeneric[Sequence[ItemT]]": ...
@overload
def transform(x: "MyGeneric[str | Sequence[ItemT]]") -> "MyGeneric[str | Sequence[ItemT]]": ...
def transform(x: "MyGeneric[str | Sequence[ItemT]]") -> "MyGeneric[str | Sequence[ItemT]]": ... # type: ignore
assert_type(transform(MyGeneric[str]()), MyGeneric[str])
assert_type(transform(MyGeneric[Sequence[int]]()), MyGeneric[Sequence[int]])
assert_type(transform(MyGeneric[str | Sequence[int]]()), MyGeneric[str | Sequence[int]])
Your Environment
- Mypy version used: 1.14.1
- Mypy command-line flags:
- Mypy configuration options from
mypy.ini(and other config files): - Python version used: 3.13.0
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia eseguendo la riproduzione fornita con mypy 1.14.1 e confermando il risultato errato di assert_type per il metodo di classe generico. Traccia la risoluzione degli overload per i metodi di classe generici, quindi aggiungi o aggiorna la copertura in modo che il TypeVar vincolato o ristretto selezioni l'overload previsto, mentre le funzioni equivalenti continuano a funzionare correttamente.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- devtools
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 38/100