Check the usage of __slots__.
Project description
Adding __slots__ to a class in Python is a great way to reduce memory usage. But to work properly, all base classes need to implement it. It turns out it’s easy to forget one class in complex inheritance trees. What’s worse: there is nothing warning you that you messed up.
✨ Until now! ✨
See my blog post for the longer story behind slotscheck.
Quickstart
Usage is quick from the command line:
slotscheck [MODULE]
For example:
$ slotscheck pandas
ERROR: 'pandas.core.internals.array_manager.SingleArrayManager' has slots but inherits from non-slot class
ERROR: 'pandas.core.internals.blocks.Block' has slots but inherits from non-slot class
ERROR: 'pandas.core.internals.blocks.NumericBlock' has slots but inherits from non-slot class
ERROR: 'pandas.core.internals.blocks.DatetimeLikeBlock' has slots but inherits from non-slot class
ERROR: 'pandas.core.internals.blocks.ObjectBlock' has slots but inherits from non-slot class
ERROR: 'pandas.core.internals.blocks.CategoricalBlock' has slots but inherits from non-slot class
ERROR: 'pandas.core.internals.array_manager.BaseArrayManager' has slots but inherits from non-slot class
ERROR: 'pandas.core.internals.managers.BaseBlockManager' has slots but inherits from non-slot class
ERROR: 'pandas.core.internals.managers.SingleBlockManager' has slots but inherits from non-slot class
Now get to fixing – and add slotscheck to your CI pipeline to prevent mistakes from creeping in again!
Use the --help option to find out more.
Could this be a flake8 plugin?
Maybe. But it’d be a lot of work.
The problem is that flake8 plugins need to work without running the code. Many libraries define conditional imports, star imports, re-exports or metaclasses which basically require running the code to find out the class tree.
There’s an issue to track any progress on 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, I’ve found in most cases this is unintentional.
Only classes at module-level are checked (i.e. no nested classes)
In rare cases imports may fail, the module is then skipped. Use the verbose mode to show detailed information.
Limited to the CPython implementation for now.
Non pure-Python classes are currently assumed to have slots. This is not necessarily the case, but it is nontrivial to determine.
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.2.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | eceb389075645a7b141a22898854bffd6b26ede1facddeaeac5fb37f301889f4 |
|
MD5 | 4b5ac3e9c76c053a1c735c166f4dfd7a |
|
BLAKE2b-256 | 80add2734a0cc5b2a0f573cc2c607587afad84f40112a8b1f8fdc6ba04057900 |