metaclass instance variable should be seen as class ClassVar
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 a metaclass has an instance variable, the class (that is an instance of the metaclass) should have that variable as a ClassVar
The class is an instance of the metaclass.
To Reproduce
from typing import Any
from typing_extensions import reveal_type
class M(type):
""" ensure classes of this type have ClassVar `x` """
x: list[int]
def __new__(mcs, name: str, bases: tuple[type, ...], dct: dict[str, Any]) -> "M":
# construct class
new_class = super().__new__(mcs, name, bases, dct)
if "x" not in dct:
new_class.x = [2]
return new_class
class C(metaclass=M):
x = [] # mypy reports "need type annotation" when it should know "ClassVar[list[int]]"
class D(metaclass=M):
pass
def f(self) -> None:
self.x = [5] # no type error reported
# should report assigning instance variable over class variable
class E(metaclass=M):
x = 3 # no type error reported - expected '"Literal[3]" is incompatible with "list[int]"'
print(C.x) # []
print(D.x) # [2]
print(E.x) # 3
reveal_type(C.x) # mypy: correctly sees list[int] pyright: list[Unknown]
reveal_type(D.x) # list[int]
reveal_type(E.x) # mypy: list[int] pyright: int
# I don't think it matters what this reports,
# because there should be a type error at assignment above.
Expected Behavior
in code comments
Actual Behavior
in code comments
Your Environment
- Mypy version used: 1.8.0
- Mypy command-line flags: --strict
- Python version used: 3.10
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
Esegui il riproduttore fornito con mypy 1.8.0 e --strict, quindi confronta le diagnostiche e i tipi rivelati con i risultati attesi nei commenti del codice. Traccia il percorso del controllo dei tipi per le variabili di istanza delle metaclassi e i corpi delle classi; il lavoro è completato quando C.x, D.x ed E.x producono il comportamento previsto relativo a ClassVar e gli errori di assegnazione.
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
- 45/100