no-slots-in-namedtuple-subclass (SLOT002)
Added in v0.0.273 · Related issues · View source
Derived from the flake8-slots linter.
What it does
Checks for subclasses of collections.namedtuple or typing.NamedTuple
that lack a __slots__ definition.
Why is this bad?
In Python, the __slots__ attribute allows you to explicitly define the
attributes (instance variables) that a class can have. By default, Python
uses a dictionary to store an object's attributes, which incurs some memory
overhead. However, when __slots__ is defined, Python uses a more compact
internal structure to store the object's attributes, resulting in memory
savings.
Subclasses of namedtuple inherit all the attributes and methods of the
built-in namedtuple class. Since tuples are typically immutable, they
don't require additional attributes beyond what the namedtuple class
provides. Defining __slots__ for subclasses of namedtuple prevents the
creation of a dictionary for each instance, reducing memory consumption.
Example
Use instead: