__slots__ to a class in Python is a great way to improve performance.
But to work properly, all base classes need to implement it — without overlap!
It’s easy to get wrong, and what’s worse: there is nothing warning you that you messed up.
✨ Until now! ✨
slotscheck helps you validate your slots are working properly.
You can even use it to enforce the use of slots across (parts of) your codebase.
See my blog post
for the origin story behind
Usage is quick from the command line:
python -m slotscheck [FILES]... # or slotscheck -m [MODULES]...
$ slotscheck -m sanic ERROR: 'sanic.app:Sanic' defines overlapping slots. ERROR: 'sanic.response:HTTPResponse' has slots but superclass does not. Oh no, found some problems! Scanned 72 module(s), 111 class(es).
Detect broken slots inheritance
Detect overlapping slots
Detect duplicate slots
(Optionally) enforce the use of slots
See the documentation for more details and configuration options.
Why not a flake8 plugin?¶
Flake8 plugins need to work without running the code. Many libraries use conditional imports, star imports, re-exports, and define slots with decorators or metaclasses. This all but requires running the code to determine the slots and class tree.
There’s an issue to discuss the matter.
slotscheckwill try to import all submodules of the given package. If there are scripts without
if __name__ == "__main__":blocks, they may be executed.
Even in the case that slots are not inherited properly, there may still be an advantage to using them (i.e. attribute access speed and some memory savings). However, in most cases this is unintentional.
slotscheckallows you to ignore specific cases.
It’s available on PyPI.
pip install slotscheck
tomlidependency on Python 3.11+
Protocolimport due to absent
typing_extensionson Python <3.10.
Protocolclasses as needing slots if strict
require-subclassoption is enabled.
TypedDictsubclasses as missing slots (#120).
The “strict-imports” default value was mistakenly
Falsein contrast to what is documented. The default value is now
True, in line with documentation. If you were relying on this permissive import behavior, you will need to set this option in the CLI or settings file.
Drop Python 3.6 support
Disabling “strict-imports” now also gracefully handles failed imports of the root module given (#113).
Add official Python 3.11 support
Fix error when encountering metaclass with slots (#106)
Improved detection of immutable classes on Python 3.10+ (#92)
Gracefully handle inspection failure due to unimportable parent package (#90).
Improved robustness of importing implicitly namespaced and extension packages (#64)
Improved robustness of determining whether a class is pure Python (#65)
Add helpful link to ‘failed to find module’ message.
setup.cfgas alternate settings file (#52).
Explicitly prevent ambiguous imports (#51).
Correctly handle case when
__slots__is defined as a single string (#47).
Small improvements to verbose error messages.
Detect duplicate slots (#21).
Improvements to docs.
Clarify pre-commit usage (#45).
Support specifying the location of settings file with
Improved verbose error messages.
Extension modules are now traversed.
Small tweaks to documentation.
Recommend running as
python -m slotscheckwhen checking files. Update pre-commit hook to reflect this.
Add Python 3.6 support
Strict imports are now the default
Include/exclude regex patterns now use partial patch (like mypy, isort do).
Clarifications in documentation
Pre-commit hook uses verbose mode by default
Arguments are now file paths. Use the
-m/--moduleoption to scan modules.
Support use as pre-commit hook.
Multiple modules or files allowed as input.
Document the types of slot errors.
Fix typo in readme.
Fix crash when encountering overlapping slots from multiple classes.
More descriptive output on overlapping slots (#26).
Simplify slot requirement flags.
allow configuration by
Recognize builtin exceptions as not having slots.
Add flags to specify inclusion as well as exclusion of modules/classes.
Allow disabling slot inheritance check.
BaseExceptionin module import.
Detect overlapping slots (#10)
100% test coverage (#15)
Improved error message if module cannot be found (#18)
Enable running with
__main__.pyin module scan to prevent running unintented code
Improve output report