python / python/mypy

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

オープン
#21,893 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

bug
主要言語
Python
スター
20.6k
フォーク
3.3k
PR マージ指標
PR 指標を取得中

説明

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

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず example.py で mypy を使ってエラーを再現し、次に、通常の宣言と dataclasses および attrs からプラグインによって生成されたスロットについて、継承された slots がどのように分類されるかを追跡します。これらのケースのカバレッジを追加し、mypy がクラスとインスタンス間のオーバーライドエラーを報告しないことを確認します。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
compilers, devtools
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
活発
明瞭さ
おおむね明確
初心者へのやさしさ
55/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。