`__slots__` is incorrectly classified as an instance variable across inheritance
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
Bug Report
Mypy classifies __slots__ as an instance variable even though Python defines it on the class. This causes a false-positive override error when a subclass explicitly annotates __slots__ as a ClassVar.
The issue also affects plugin-generated __slots__, including slots generated by dataclasses and attrs.
To Reproduce
from dataclasses import dataclass
from typing import ClassVar
@dataclass(slots=True)
class Base:
x: float
y: float
class Child(Base):
__slots__: ClassVar[tuple[()]] = ()
Run:
mypy example.py
Playground link: https://mypy-play.net/?mypy=latest&python=3.14&gist=e0e2f906ab9e0201c43d2b5cb6ff950c
Expected Behavior
Mypy should report no errors. Both generated and explicitly declared __slots__ should be treated as class variables across inheritance.
Actual Behavior
Cannot override instance variable (previously declared on base class "Base") with class variable [misc]
A # type: ignore[misc] comment is currently required on the subclass declaration even though __slots__ is a class-level attribute.
The same inconsistent classification occurs with ordinary __slots__ declarations and attrs-generated slots.
Your Environment
- Mypy version used: 2.3.1
- Mypy command-line flags: none
- Mypy configuration options from
mypy.ini(and other config files): none - Python version used: 3.14.7
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, den Fehler mit mypy in example.py zu reproduzieren, und verfolge dann, wie geerbte slots für gewöhnliche Deklarationen und von Plugins erzeugte Slots aus dataclasses und attrs klassifiziert werden. Füge Abdeckung für diese Fälle hinzu und überprüfe, dass mypy keinen Fehler wegen einer Überschreibung zwischen Klasse und Instanz meldet.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- compilers, devtools
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Aktiv
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 55/100