Skip to main content

PEP 690 like lazy imports for python

Project description

ci Docs pypi version Python Versions PyPI - Downloads coverage GitHub Sponsors

lazy-imports-lite changes the semantics of python imports and defers the import until it is used the first time like in PEP 690.

I liked the idea of lazy imports and wanted to use them for my projects without having to change my code. I hope this project allows more people to use lazy imports in their projects.

[!NOTE] It is important to note that this project is not affiliated in any way with the original PEP. I hope this project allows a wider adoption of lazy imports, but there is currently no plan to standardize it.

[!IMPORTANT] lazy-imports-lite is still in early development and may contain bugs. Make sure to test your code carefully before you use it.

Key Features

  • lazy imports almost like PEP 690
  • no code changes required
  • can be enabled per module (by keyword in the package)

How is it different to PEP 690?

  • It has not the same performance as the implementation from the pep. Every access to parts of imported modules is transformed to an attribute access x -> x._lazy_value.
  • Exceptions during deferred import are converted to LazyImportError

Usage

  • add lazy-imports-lite to your project dependencies.
  • add lazy-imports-lite-enabled to the keywords of your pyproject.toml.
      [project]
      keywords=["lazy-imports-lite-enabled"]
    

This enables lazy imports for all top-level imports in your modules in your project. One way to verify if it is enabled is to check which loader is used.

>>> import your_project
>>> print(type(your_project.__spec__.loader))
<class 'lazy_imports_lite._loader.LazyLoader'>

Implementation

lazy-imports-lite works by rewriting the AST at runtime before the code is compiled.

The following code:

from foo import bar


def f():
    print(bar())

is internally transformed to:

import lazy_imports_lite._hooks as __lazy_imports_lite__

globals = __lazy_imports_lite__.make_globals(lambda g=globals: g())
bar = __lazy_imports_lite__.ImportFrom(__package__, "foo", "bar")


def f():
    print(bar._lazy_value())

This transformation should be never visible to you (the source location is preserved) but it is good to know if something does not work as expected.

You can view a preview of this transformation with lazy-imports-lite preview <filename> if you want to know how your code would be changed.

TODO

  • handle exec/eval
  • mutable globals()

Issues

If you encounter any problems, please report an issue along with a detailed description.

License

Distributed under the terms of the MIT license, "lazy-imports-lite" is free and open source software.

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

lazy_imports_lite-0.1.0.tar.gz (70.7 kB view details)

Uploaded Source

Built Distribution

lazy_imports_lite-0.1.0-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

Details for the file lazy_imports_lite-0.1.0.tar.gz.

File metadata

  • Download URL: lazy_imports_lite-0.1.0.tar.gz
  • Upload date:
  • Size: 70.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.25.1

File hashes

Hashes for lazy_imports_lite-0.1.0.tar.gz
Algorithm Hash digest
SHA256 7aa4b98eb0677447c5f62c59803b6f119c2d58124bc6d6208dd0f198bee46254
MD5 875c10ec28448689b443b28ae41a071a
BLAKE2b-256 44982f9fc7d1ec0cb0e0cabe1aff6bcf1cd365698451fb52b55574e0b2982143

See more details on using hashes here.

File details

Details for the file lazy_imports_lite-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for lazy_imports_lite-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1dcb9bd761f7ac0de4e11b079907e0d240de84a93480f5f53607b424f40e8319
MD5 5a57e5ca7e03c972926bbd74b368d900
BLAKE2b-256 f7d043f5733795290c094080098fab4955d260bf77626b7ab752c402d848781f

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