`__slots__` is incorrectly classified as an instance variable across inheritance
还没有人认领这个 Issue。
评估
调研方向
首先在 example.py 上使用 mypy 重现该错误,然后跟踪继承的 slots 如何针对普通声明以及由 dataclasses 和 attrs 通过插件生成的 slots 进行分类。为这些情况添加覆盖,并验证 mypy 不会报告类与实例之间的覆盖错误。
由索引模型根据 Issue 内容生成。
描述
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
- 主要语言
- Python
- 星标
- 20.6k
- 派生
- 3.3k
- 平均合并
- 1 天 12 小时
- 30 天内合并 PR
- 58
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
python/mypy 的其他 Issue
-
bug
难度 2/5 1-3 小时 新手友好度 78/100
-
bug
难度 2/5 1-3 小时 新手友好度 76/100
-
documentation
难度 2/5 1-3 小时 新手友好度 72/100
-
bug topic-configuration topic-error-reporting
难度 2/5 1-3 小时 新手友好度 68/100
-
bug topic-attrs
难度 2/5 1-3 小时 新手友好度 62/100
相似的 Issue
-
essnmx good first issue
难度 1/5 1 小时以内 新手友好度 95/100
-
难度 2/5 1-3 小时 新手友好度 65/100
syfoud/Simulated_Scepter#174 ·
-
难度 2/5 1-3 小时 新手友好度 75/100
Giskard-AI/giskard-oss#2840 · 1 条评论 ·
-
A claim comment carrying the issue number is silently declined while the workflow reports success 未关闭area: repo bug perceived difficulty: 2
难度 2/5 1-3 小时 新手友好度 70/100
-
难度 2/5 1-3 小时 新手友好度 75/100
yeti-platform/yeti#1380 ·