Skip to main content

Super-easy lazy importing in Python.

Project description

slothy
skeleton Supported Python versions Package version

Tests Coverage Documentation Status

Super-easy lazy importing in Python.

[!Important] Only CPython is supported.

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 pandas import DataFrame

# pandas.DataFrame not imported

def main() -> None:
    # pandas.DataFrame not imported
    print(DataFrame)  # <class 'pandas.core.frame.DataFrame'>
    # pandas.DataFrame imported just before print() called; from now on,
    # available everywhere in the module.


if __name__ == "__main__":
    main()

Caveats

Python <3.10 Boilerplate

On Python 3.8-3.9, every function accessing a lazily-imported object must be decorated with supports_slothy:

from slothy import SLOTHY, supports_slothy

with SLOTHY:
    from pandas import DataFrame

# pandas.DataFrame not imported

@supports_slothy
def main() -> None:
    # pandas.DataFrame not imported
    print(DataFrame)  # <class 'pandas.core.frame.DataFrame'>
    # pandas.DataFrame imported just before print() called; from now on,
    # available everywhere in the module.


if __name__ == "__main__":
    main()

Deleted References

Importing symbols with SLOTHY will make them intentionally unavailable in your namespace after the with SLOTHY block finishes.

Consequently,

from slothy import SLOTHY

with SLOTHY:
    from pandas import DataFrame

try:
    DataFrame
except NameError:
    print("DataFrame undefined")

outputs DataFrame undefined. This is caused by the fact that there must be at least 1 frame between "declared import" and usage. More in-depth technical explanation will be provided soon.

Credits

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

Poetry Ruff Pre-commit

[!Note] If you use Windows, it is highly recommended to complete the installation in the way presented below through WSL2.

  1. Fork the slothy repository on GitHub.

  2. 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
    
  3. 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


Download files

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

Source Distribution

slothy-0.1.0b0.tar.gz (13.8 kB view details)

Uploaded Source

Built Distribution

slothy-0.1.0b0-py3-none-any.whl (11.9 kB view details)

Uploaded Python 3

File details

Details for the file slothy-0.1.0b0.tar.gz.

File metadata

  • Download URL: slothy-0.1.0b0.tar.gz
  • Upload date:
  • Size: 13.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for slothy-0.1.0b0.tar.gz
Algorithm Hash digest
SHA256 a2f045633e6d1495597953c60d2e47cdfffcf07cc3a14f8b7dc55518a3b3d84f
MD5 09bffb10b6c91dee90880b66bc083316
BLAKE2b-256 62b47ce9e4f24e6d7c3e893b0eace07609e1ade5f35c577907b4cf97859ddd1e

See more details on using hashes here.

File details

Details for the file slothy-0.1.0b0-py3-none-any.whl.

File metadata

  • Download URL: slothy-0.1.0b0-py3-none-any.whl
  • Upload date:
  • Size: 11.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for slothy-0.1.0b0-py3-none-any.whl
Algorithm Hash digest
SHA256 bacab6c37873a1e67a3d3fd2717c51ad6e42d65e776c4736dbd15f0e6fa6dc6a
MD5 b185428b65b9a7babf447b66e75c6748
BLAKE2b-256 b515e683233fa8ad69bf8a48f889a9394f4e7906f635fcbe4ba3489282ad4cdc

See more details on using hashes here.

Supported by

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