Super-easy lazy importing in Python.
Project description
Super-easy lazy importing in Python.
Intended to be used as a drop-in replacement for if typing.TYPE_CHECKING
blocks
as well as a convenient guard against expensive imports.
Usage
>>> from slothy import slothy
>>>
>>> with slothy():
... from functools import partial, reduce
... print(partial) # <from functools import partial, ...>
... print(reduce) # <from functools import ..., reduce>
>>>
>>> # First time imported items are referenced, they're imported.
... # If a declared item is never referenced, the module containing it is never
... # imported too, provided it was not imported by some external instruction.
... partial, reduce
(<class 'functools.partial'>, <built-in function reduce>)
slothy()
will default to eager imports on unsupported Python implementations,
i.e. those that don't define sys._getframe
. While this library is 3.8+
and that eliminates the risk of running slothy in most of these implementations now,
future versions of them can finally support 3.8.
To prevent eager imports in with slothy()
statements and instead fail on unsupported
Python implementations, use slothy(prevent_eager=True)
. Preventing eager imports might
be useful in type-checking sections, where eager imports could cause cycles resulting
in less readable tracebacks.
Credits
Many thanks to Jelle Zijlstra @JelleZijlstra who wrote a basic dict key lookup-based lazy importing implementation that is now the core solution of slothy.
Special thanks to Carl Meyer @carljm who willingly sacrificed his time to consult the project with me and share his deep knowledge of the problem at the bigger picture. His experience with PEP 690 as a Meta software engineer helped me grasp the topic in the context of real production. I find it delightful!
Kudos to Alex Waygood @AlexWaygood who made this project possible by sharing his knowledge of CPython implementation details regarding name lookup behavior.
Shoutout to Will McGugan @willmcgugan who supported the idea of slothy from the very beginning and promoted the project on Twitter.
Installation
You might simply install it with pip:
pip install slothy
If you use Poetry, then you might want to run:
poetry add slothy
For Contributors
[!Note] If you use Windows, it is highly recommended to complete the installation in the way presented below through WSL2.
-
Fork the slothy repository on GitHub.
-
Install Poetry.
Poetry is an amazing tool for managing dependencies & virtual environments, building packages and publishing them. You might use pipx to install it globally (recommended):pipx install poetry
If you encounter any problems, refer to the official documentation for the most up-to-date installation instructions.
Be sure to have Python 3.8 installed—if you use pyenv, simply run:
pyenv install 3.8
-
Clone your fork locally and install dependencies.
git clone https://github.com/your-username/slothy path/to/slothy cd path/to/slothy poetry env use $(cat .python-version) poetry install
Next up, simply activate the virtual environment and install pre-commit hooks:
poetry shell pre-commit install
For more information on how to contribute, check out CONTRIBUTING.md.
Always happy to accept contributions! ❤️
Legal Info
© Copyright by Bartosz Sławecki (@bswck).
This software is licensed under the terms of MIT License.
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
File details
Details for the file slothy-0.2.0b1.tar.gz
.
File metadata
- Download URL: slothy-0.2.0b1.tar.gz
- Upload date:
- Size: 11.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.0.0 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 042e220468c5f19c525f884a14b895bdc86e3bef3d847dd239afc1104f508001 |
|
MD5 | af96d88a3dbfd90b2acd1cab929d1ff2 |
|
BLAKE2b-256 | 76318b9ddd06f57897c13f3144e47a2461220e2b6cd6945769542af935b36293 |
File details
Details for the file slothy-0.2.0b1-py3-none-any.whl
.
File metadata
- Download URL: slothy-0.2.0b1-py3-none-any.whl
- Upload date:
- Size: 9.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.0.0 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 14e1e4c99686b4e2ab4cf77ab2b349bcc8b806eb8c9b4a23faf906ccbcd98d41 |
|
MD5 | 6d5ad2919d092e2e2a27d2bfad7a0892 |
|
BLAKE2b-256 | 9676eed892fa6749c51645d574dbd01a1507dc116da08d242546b1219f9be092 |