Skip to content

no-slots-in-namedtuple-subclass (SLOT002)#

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#

from collections import namedtuple


class Foo(namedtuple("foo", ["str", "int"])):
    pass

Use instead:

from collections import namedtuple


class Foo(namedtuple("foo", ["str", "int"])):
    __slots__ = ()

References#