Ensure your __slots__ are working properly.
Project description
Adding __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 slotscheck.
Quickstart
Usage is quick from the command line:
python -m slotscheck [FILES]...
# or
slotscheck -m [MODULES]...
For example:
$ 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).
Now get to fixing — and add slotscheck to your CI pipeline or pre-commit to prevent mistakes from creeping in again! See here and here for examples.
Features
Detect broken slots inheritance
Detect overlapping slots
Detect duplicate slots
Pre-commit hook
(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.
Notes
slotscheck will 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. slotscheck allows you to ignore specific cases.
Installation
It’s available on PyPI.
pip install slotscheck
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for slotscheck-0.16.4-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2a4f63debc66578e281f87bb581929f416788005a9260c2da4520a0f82a3b860 |
|
MD5 | 3d8ef8888c356c75395c8053a45cadb4 |
|
BLAKE2b-256 | 128eb4b6bbb3a9ffd9f1096c78ed41ab4c174cd0a6bdda889cba5ff473629a80 |