Slotscheck
Slotscheck is a Python library that helps ensure your `__slots__` are working properly. It acts as a linter, identifying classes where `__slots__` might not be effective or are misused, thereby aiding in memory optimization and performance. The current version is 0.19.1, and it has a regular release cadence, often aligning with new Python versions.
Common errors
-
TypeError: 'module' object is not callable
cause You imported the entire `slotscheck` module (e.g., `import slotscheck`) and then tried to call the module itself as a function (e.g., `slotscheck(MyClass)`).fixImport the specific function directly: `from slotscheck import slotscheck`. Then call it: `slotscheck(MyClass)`. Alternatively, if you `import slotscheck`, you must call `slotscheck.slotscheck(MyClass)`. -
AttributeError: module 'slotscheck' has no attribute 'check_slots'
cause You're attempting to call a function named `check_slots`, which does not exist in the `slotscheck` library. The primary function for checking is also named `slotscheck`.fixUse the correct function name: `from slotscheck import slotscheck` followed by `slotscheck(MyClass)`. -
ImportError: cannot import name 'slotscheck' from 'slotscheck' (possibly due to a circular import)
cause This error often occurs if you have a local Python file named `slotscheck.py` in your project directory, which conflicts with the installed `slotscheck` package.fixRename your local file if it's named `slotscheck.py` to avoid name collisions. Ensure you're importing from the installed library, not a local file.
Warnings
- breaking Python 3.8 support was dropped in `slotscheck` version `0.19.1`. If you are using Python 3.8, you must upgrade your Python interpreter or pin your `slotscheck` version.
- breaking Python 3.7 support was dropped in `slotscheck` version `0.17.0`. Users on Python 3.7 will not be able to install or use newer versions.
- gotcha When using Python 3.12+, `Generic` classes could be incorrectly flagged as not having slots in `slotscheck` versions prior to `0.17.1`, leading to false positives.
- gotcha `TypedDict` imported from `typing_extensions` could be incorrectly flagged as not having slots in `slotscheck` versions prior to `0.16.5`, particularly in Python versions where `TypedDict` is already in the standard library.
Install
-
pip install slotscheck
Imports
- slotscheck
from slotscheck import slotscheck
Quickstart
from slotscheck import slotscheck
class MySlottedClass:
__slots__ = ('x', 'y')
def __init__(self, x, y):
self.x = x
self.y = y
class MyUnslottedClass:
def __init__(self, x, y):
self.x = x
self.y = y
results_slotted = slotscheck(MySlottedClass)
print(f"MySlottedClass check results: {results_slotted}")
results_unslotted = slotscheck(MyUnslottedClass)
print(f"MyUnslottedClass check results: {results_unslotted}")
# To check an entire module (e.g., the current file):
# import sys
# current_module = sys.modules[__name__]
# module_results = slotscheck(current_module)
# print(f"Current module check results: {module_results}")