no-slots-in-str-subclass (SLOT000)
Added in v0.0.273 · Related issues · View source
Derived from the flake8-slots linter.
What it does
Checks for subclasses of str 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 str inherit all the attributes and methods of the built-in
str class. Since strings are typically immutable, they don't require
additional attributes beyond what the str class provides. Defining
__slots__ for subclasses of str prevents the creation of a dictionary
for each instance, reducing memory consumption.
Example
Use instead: