Skip to content

__slots__ is incorrectly classified as an instance variable across inheritance #21893

Description

@XuehaiPan

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions