Skip to content

PLE0237 false positive #10306

@henryiii

Description

@henryiii

Currently, PLE0237 will report "Attribute other is not defined in class's __slots__" on a class like this:

class A:
    __slots__ = ("pub",)

    def __init__(self) -> None:
        self.other = "hi"

Which is correct. But it also reports the same error on the following correct class:

class B:
    __slots__ = ("pub", "__dict__")

    def __init__(self) -> None:
        self.other = "hi"

This is incorrect; since the class has a __dict__ in it's __slots__, it has a dict to put other in. This is described in https://docs.python.org/3/reference/datamodel.html#slots (both __dict__ and __weakref__ can be declared in __slots__), and is an important technique for wrapper classes, where the wrapped object is declared in slots, so that the __dict__ has everything except the wrapped object.

Incorrect check result seen in scikit-hep/boost-histogram#914.

  • Command ruff check --select=ALL --isolated tmp.py
  • Ruff version: ruff 0.3.1

Metadata

Metadata

Assignees

Labels

bugSomething isn't workinglinterRelated to the linter

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions