Skip to content

Check "__slots__" and "@dataclass" attributes #9499

Open
@buhtz

Description

@buhtz

Current problem

The following is valid code but implicates some problems.

from dataclasses import dataclass

@dataclass
class MyClass:
    __slots__ = ('foo', 'bar', )
    foo: str

    def __init__(self):
        self.foo = 'one'
        self.bar = 'two'

There are two slot variables (foo & bar) but only foo is handled by the @dataclass. For example str(MyClass()) will result in MyClass(foo="one"). The variable bar is missing in that output no matter that it exists.

Desired solution

PyLint should warn about that.

The list of variables in __slot__ do not fit to the list specificied for @dataclass.

Python 3.10 or higher to offer @dataclass(slots=True) to prevent situations like this. So this could be one possible solution PyLint could suggest. But there are also older Python versions still relevant.

Additional context

No response

Metadata

Metadata

Assignees

Labels

Enhancement ✨Improvement to a componentNeeds decision 🔒Needs a decision before implemention or rejection

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions