Skip to main content

Super-easy lazy importing in Python.

Project description

slothy
skeleton Supported Python versions Package version

Tests Coverage

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 lazy_importing
>>>
>>> with lazy_importing(prevent_eager=False):
...     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>)

lazy_importing() 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 lazy_importing() statements and instead fail immediately on unsupported Python implementations, use with lazy_importing(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

Rye 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-1.0.0b0.tar.gz (12.4 kB view details)

Uploaded Source

Built Distribution

slothy-1.0.0b0-py3-none-any.whl (10.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: slothy-1.0.0b0.tar.gz
  • Upload date:
  • Size: 12.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.0 CPython/3.12.4

File hashes

Hashes for slothy-1.0.0b0.tar.gz
Algorithm Hash digest
SHA256 54aa88c19903e24afdc3a6bea2c7c7027bee530eebe577c98424f031c7db430c
MD5 89f7f3dc31efbfbabae727b9048edcf3
BLAKE2b-256 d44fe1266fd1700ac3798a12c2a47dca05ae101ba9dcbb6e399ee73f1998d6ad

See more details on using hashes here.

File details

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

File metadata

  • Download URL: slothy-1.0.0b0-py3-none-any.whl
  • Upload date:
  • Size: 10.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.0 CPython/3.12.4

File hashes

Hashes for slothy-1.0.0b0-py3-none-any.whl
Algorithm Hash digest
SHA256 d2f9b4609bd5290e24f71741683e8e1193e21baa63d6b0f2ab1c2700cca37176
MD5 aaaeca3c3efeb18f714e8bdbb3d525f6
BLAKE2b-256 49b0476cd1181b87184fa75a920a7e87865de0886d85f0f3ff9e36c126f4b2c2

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