Skip to main content
https://img.shields.io/pypi/v/slotscheck.svg?color=blue https://img.shields.io/pypi/l/slotscheck.svg https://img.shields.io/pypi/pyversions/slotscheck.svg https://img.shields.io/readthedocs/slotscheck.svg https://github.com/ariebovenberg/slotscheck/actions/workflows/build.yml/badge.svg https://img.shields.io/badge/code%20style-black-000000.svg

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:BaseArrayManager' has slots but inherits from non-slot class.
ERROR: 'pandas.core.internals.array_manager:SingleArrayManager' defines overlapping slots.
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:CategoricalBlock' 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:NumericBlock' 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.managers:BaseBlockManager' has slots but inherits from non-slot class.
ERROR: 'pandas.core.internals.managers:SingleBlockManager' has slots but inherits from non-slot class.
Oh no, found some problems!

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 use conditional imports, star imports, re-exports, and define slots with decorators or metaclasses. This all but requires running the code to determine the class tree and slots.

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.

  • 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

Release files for slotscheck 0.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for slotscheck 0.4.0
File Size Uploaded
slotscheck-0.4.0.tar.gz 11.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for slotscheck 0.4.0
File Interpreter ABI Platform
slotscheck-0.4.0-py3-none-any.whl Python 3 none any Details

Total release size: 24.4 kB

Release files / slotscheck-0.4.0.tar.gz

Download URL slotscheck-0.4.0.tar.gz
Size 11.4 kB
Tags Source
SHA-256 checksum
How to use checksums
3974a8fe93f395e5a305e3d9fc31b394d4757da966b591bf388bd6971244f192
BLAKE2b-256 checksum
How to use checksums
b59b89e24a402ad8056e014e8ecc17b7aa1baddc95d97cf34779e5328e488626
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.12 CPython/3.9.9 Darwin/21.2.0

Release files / slotscheck-0.4.0-py3-none-any.whl

Download URL slotscheck-0.4.0-py3-none-any.whl
Size 13.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e109a6e6322cd60d7cb11d5d8710f74e45e40addd18f34b8a349150814207130
BLAKE2b-256 checksum
How to use checksums
8d58c8fd2bd49ed76ac12c8cb569a9d133ceeb59f2cc445125a804407cb07c64
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.12 CPython/3.9.9 Darwin/21.2.0

Release history Release notifications | RSS feed

0.20.0

2 release files

0.19.1

2 release files

0.19.0

2 release files

0.18.0

2 release files

0.17.0

2 release files

0.16.4

2 release files

0.16.2

2 release files

0.16.1

2 release files

0.16.0

2 release files

0.14.1

2 release files

0.14.0

2 release files

0.13.0

2 release files

0.12.1

2 release files

0.10.2

2 release files

0.10.1

2 release files

0.10.0

2 release files

0.9.0

2 release files

0.8.0

2 release files

0.7.2

2 release files

0.7.1

2 release files

0.7.0

2 release files

0.6.0

2 release files

0.5.3

2 release files

0.5.2

2 release files

0.5.1

2 release files

0.5.0

2 release files

This release

0.4.0 This release

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page