Skip to main content

lazyimp

Project description

Overview

Extracted from omlish.

This library provides easy to use lazy import mechanisms powered in various ways by PEP-562 module-level __getattr__ hooks.

Mechanisms

proxy_import

Returns anonymous types.ModuleType instances with __getattr__ hooks to lazily import requested contents upon access.

For relative imports, __package__ must be provided.

Additional 'extra' submodules to be imported may be specified with the extras kwarg.

Limited to importing whole modules (as opposed to importing individual items out of an imported module's globals), but usable anywhere. The reason for this limitation is that module-level __getattr__ hooks cannot be used to intercept access to intra-module globals:

Looking up a name as a module global will bypass module getattr. This is intentional, otherwise calling getattr for builtins will significantly harm performance.

As a result the module globals provided by proxy_import must remain 'fixed' in the globals dict. While it would be technically possible to return 'proxy' (or otherwise radically polymorphic) objects to simulate lazy attr imports, I don't consider that a good idea.

For typechecking and other tooling, the real imports should be diligently duplicated above the proxy_imports in a conditional TYPE_CHECKING block.

if typing.TYPE_CHECKING:
    import foo
    
    import thing.a
    import thing.b.c
    
    from . import bar
    from .baz import qux
    
else:
    foo = lazyimp.proxy_import('foo')
    
    thing = lazyimp.proxy_import('thing', extras=['a', 'b.c'])
    
    bar = lazyimp.proxy_import('.bar', __package__)
    qux = lazyimp.proxy_import('.bar.qux', __package__)

auto_proxy_import

Powered by proxy_import, but uses the import capture mechanism to automatically capture and proxy imported modules. No TYPE_CHECKING conditional block is necessary, but still limited to importing whole modules.

with lazyimp.auto_proxy_import(globals()):
    import foo
    
    import thing.a
    import thing.b.c
    
    from . import bar
    from .baz import qux

proxy_init

Intended for use primarily in package __init__.py modules. Installs a lazy globals module-level __getattr__ hook and uses it to appropriately redirect imported items to underlying anonymous types.ModuleType instances. Unlike proxy_import it is capable of importing individual attrs from imported modules.

It is passed globals() for installing its hook, and thus doesn't need to be passed __package__ for relative imports.

It may safely be called multiple times without overwriting a previously installed hook.

It supports import aliases by being passed a (real_name, alias_name) pair, but does not support star imports.

if typing.TYPE_CHECKING:
    from math import pi, theta as not_theta
    
    from . import foo
    from .bar import baz, qux

else:
    lazyimp.proxy_init(globals(), 'math', [
        'pi',
        ('theta', 'not_theta'),
    ])

    lazyimp.proxy_init(globals(), '.foo')

    lazyimp.proxy_init(globals(), '.bar', [
        'baz',
        'qux',
    ])

auto_proxy_init

Powered by proxy_init, but uses the import capture mechanism to automatically capture and proxy imported modules. As with auto_proxy_init no TYPE_CHECKING conditional block is necessary.

with lazyimp.auto_proxy_init(globals()):
    from math import pi, theta as not_theta
    
    from . import foo
    from .bar import baz, qux

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

lazyimp-0.0.3.tar.gz (12.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

lazyimp-0.0.3-cp313-cp313-macosx_15_0_arm64.whl (15.2 kB view details)

Uploaded CPython 3.13macOS 15.0+ ARM64

File details

Details for the file lazyimp-0.0.3.tar.gz.

File metadata

  • Download URL: lazyimp-0.0.3.tar.gz
  • Upload date:
  • Size: 12.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for lazyimp-0.0.3.tar.gz
Algorithm Hash digest
SHA256 a5a1dffabc850c8636a7cb6f81437d220c64cb73ffb8bb168aabb5041459c59e
MD5 b7321cfc284a191b79d1beb81904b39a
BLAKE2b-256 b7bad7c56736c0e04e5308e0548848f49e65aa8090a768c75a7a559fcb18f44c

See more details on using hashes here.

File details

Details for the file lazyimp-0.0.3-cp313-cp313-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for lazyimp-0.0.3-cp313-cp313-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 0cc614b1494afd8959886997c8bb913e55883ef3757f9d2c217d911fa2ef83eb
MD5 26fecefadae0180139281c6a6400ca25
BLAKE2b-256 f85b66c26c04e8cafce21410ba6a8650eaa072010db6dd3c606f5b7dfd526854

See more details on using hashes here.

Supported by

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