Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ariebovenberg/slotscheck
🎰 Ensure your __slots__ are working properly
https://github.com/ariebovenberg/slotscheck
Last synced: 7 days ago
JSON representation
🎰 Ensure your __slots__ are working properly
- Host: GitHub
- URL: https://github.com/ariebovenberg/slotscheck
- Owner: ariebovenberg
- License: mit
- Created: 2021-12-29T16:56:38.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2024-04-08T10:56:02.000Z (9 months ago)
- Last Synced: 2024-04-08T11:50:54.308Z (9 months ago)
- Language: Python
- Homepage: https://slotscheck.rtfd.io
- Size: 617 KB
- Stars: 85
- Watchers: 4
- Forks: 8
- Open Issues: 16
-
Metadata Files:
- Readme: README.rst
- Changelog: CHANGELOG.rst
- License: LICENSE
Awesome Lists containing this project
- awesome-python-testing - slotscheck - Find mistakes in your `__slots__` definitions. (Static Checks)
README
🎰 Slotscheck
=============.. image:: https://img.shields.io/pypi/v/slotscheck.svg?color=blue
:target: https://pypi.python.org/pypi/slotscheck.. image:: https://img.shields.io/pypi/l/slotscheck.svg
:target: https://pypi.python.org/pypi/slotscheck.. image:: https://img.shields.io/pypi/pyversions/slotscheck.svg
:target: https://pypi.python.org/pypi/slotscheck.. image:: https://img.shields.io/readthedocs/slotscheck.svg
:target: http://slotscheck.readthedocs.io/.. image:: https://github.com/ariebovenberg/slotscheck/actions/workflows/build.yml/badge.svg
:target: https://github.com/ariebovenberg/slotscheck/actions/workflows/build.yml.. image:: https://img.shields.io/codecov/c/github/ariebovenberg/slotscheck.svg
:target: https://codecov.io/gh/ariebovenberg/slotscheck.. image:: https://img.shields.io/badge/code%20style-black-000000.svg
:target: https://github.com/psf/blackAdding ``__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:
.. code-block:: bash
python -m slotscheck [FILES]...
# or
slotscheck -m [MODULES]...For example:
.. code-block:: bash
$ 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 slotsSee `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.
- Because ``slotscheck`` imports your code in arbitrary order,
it can—in rare cases—result in confusing and randomly-occurring import errors
in third-party libraries.
In such a case, it is recommended to omit modules such as your tests
and mypy plugins from the slotscheck run.
See `here `_.
Alternatively, you can use ``PYTHONHASHSEED=`` to make the import order deterministic.
A solution to this problem is being worked on in `this issue `_.Installation
------------It's available on PyPI.
.. code-block:: bash
pip install slotscheck