Usage¶
This library provides the Flake8 plugin flake8-slots
to require __slots__ to be defined for subclasses of immutable types.
Flake8 codes¶
Code |
Description |
---|---|
SLOT000 |
Define __slots__ for subclasses of str |
SLOT001 |
Define __slots__ for subclasses of tuple |
SLOT002 |
Define __slots__ for subclasses of collections.namedtuple |
For subclasses of immutable types it is recommended to use __slots__ = ()
to keep the memory usage down.
For subclasses of str
it is possible to define other variables which may be set on instances. For example:
class MyString(str):
__slots__ = ("a_variable", )
which will allow a value to be assigned to a_variable
.
Pre-commit hook¶
flake8-encodings
can also be used as a pre-commit
hook
See pre-commit for instructions
Sample .pre-commit-config.yaml
:
- repo: https://github.com/pycqa/flake8
rev: 3.8.4
hooks:
- id: flake8
additional_dependencies:
- flake8-slots==0.1.6