Skip to main content

Ensure your __slots__ are working properly.

Project description

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/codecov/c/github/ariebovenberg/slotscheck.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 — 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 to prevent mistakes from creeping in again! See here and here for examples.

Features

  • Detect broken slots inheritance

  • Detect overlapping 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.

  • 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

slotscheck-0.10.0.tar.gz (15.5 kB view details)

Uploaded Source

Built Distribution

slotscheck-0.10.0-py3-none-any.whl (17.7 kB view details)

Uploaded Python 3

File details

Details for the file slotscheck-0.10.0.tar.gz.

File metadata

  • Download URL: slotscheck-0.10.0.tar.gz
  • Upload date:
  • Size: 15.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.12 CPython/3.9.9 Darwin/21.3.0

File hashes

Hashes for slotscheck-0.10.0.tar.gz
Algorithm Hash digest
SHA256 8307549d0b152a3ac7a05e081b85d18aa2897759c0a6866a4ad69e86e9010056
MD5 3f1f5aa4c75a40cc259dfd72968fe173
BLAKE2b-256 dfae1d4b583c8d4d1ccffd4675c375e4c330bdc99714a7301cff8847569e94c4

See more details on using hashes here.

File details

Details for the file slotscheck-0.10.0-py3-none-any.whl.

File metadata

  • Download URL: slotscheck-0.10.0-py3-none-any.whl
  • Upload date:
  • Size: 17.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.12 CPython/3.9.9 Darwin/21.3.0

File hashes

Hashes for slotscheck-0.10.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4476edecfd42a2b8934d32cc0c660e015220d5a2b5c753c8bdac569ee7c4d621
MD5 533401ec77f46aca6aad1a6e379bd9ca
BLAKE2b-256 758c043cb62101fae5cb9db62e14010801f38376389d51bb6009d1a050289a7b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page