Hacktoberfest 2026:維護者為十月標記出來的 issue,仍然開放、適合新手。 瀏覽 Hacktoberfest issue

`__slots__` is incorrectly classified as an instance variable across inheritance

未關閉
#21,893 1 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

維護者通常 1 天內回覆

@XuehaiPan 已經在處理了。

開始於 2026年8月24日。

  • #21886 來自 @XuehaiPan —— 未關閉

評估

難度
4/5
預估耗時
3-5 天
新手友好度
55/100
Issue 類型
缺陷
描述清晰度
基本清楚
活躍度
活躍
技術堆疊
python

研究方向

先在 example.py 上使用 mypy 重現錯誤,然後追蹤繼承的 slots 如何針對一般宣告,以及由 dataclasses 和 attrs 透過 plugin 產生的 slots 進行分類。為這些情況新增覆蓋,並確認 mypy 不會回報類別與實例之間的覆寫錯誤。

由索引模型根據 Issue 內容生成。

描述

bug

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.7k
分支
3.3k
平均合併
2 天 1 小時
30 天內合併 PR
104

環境準備

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

python/mypy 的其他 Issue

查看 python/mypy 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。