Inconsistent Handling of Self in ClassVar Annotations
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
When using Self within a ClassVar, mypy raises an arg-type error. This behavior seems inconsistent with the intended use of Self and ClassVar.
To Reproduce
MyPy playground: https://mypy-play.net/?mypy=latest&python=3.12&gist=dfe06f06e69d157317a667311aa75952
from typing import TypeVar, Generic, ClassVar, Self
T = TypeVar("T", bound="BaseModel")
class BaseModel:
_meta: "ClassVar[Meta[Self]]"
class Meta(Generic[T]):
"""Metadata class for BaseModel."""
def __init__(self, model: type[T]) -> None:
self.model = model
@classmethod
def __init_subclass__(cls) -> None:
# The following line triggers mypy error "arg-type"
# bug.py: note: In member "__init_subclass__" of class "BaseModel":
# bug.py:21:30: error: Argument 1 to "Meta" has incompatible type "type[BaseModel]"; expected "type[Self]" [arg-type]
cls._meta = cls.Meta(cls)
# outputs --> Revealed type is "bug.BaseModel.Meta[bug.BaseModel]"
reveal_type(BaseModel._meta)
Expected Behavior
The code should type-check without errors, as Self within the ClassVar should correspond to the subclass type, making cls.Meta(cls) a valid instantiation.
Actual Behavior
mypy raises an arg-type error
bug.py: note: In member "__init_subclass__" of class "BaseModel":
bug.py:21:30: error: Argument 1 to "Meta" has incompatible type "type[BaseModel]"; expected "type[Self]" [arg-type]
Your Environment:
mypy version used: 1.15.0
Python version used: 3.12.8
Additional Context:
Removing ClassVar, or changing Self to BaseModel both eliminate the mypy error.
This issue may be related to or a duplicate of Issue #17395, where Self types in ClassVar annotations lose specificity through TypeVar. In that case, Self appears to decay to the TypeVar bound instead of retaining the specific subclass type. I'm not certain if these are the same issue.
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 con il reproducer del playground di MyPy usando mypy 1.15.0 e Python 3.12.8, quindi confronta il comportamento con l’issue #17395. Traccia come viene gestito Self all’interno dell’annotazione ClassVar e della chiamata a init_subclass. Il lavoro è completato quando l’esempio supera il controllo dei tipi senza l’errore arg-type e dispone di copertura di regressione.
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
- 42/100