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_import 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.6.tar.gz (16.0 kB view details)

Uploaded Source

Built Distributions

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

lazyimp-0.0.6-cp314-cp314-macosx_15_0_arm64.whl (18.5 kB view details)

Uploaded CPython 3.14macOS 15.0+ ARM64

lazyimp-0.0.6-cp313-cp313-macosx_15_0_arm64.whl (18.5 kB view details)

Uploaded CPython 3.13macOS 15.0+ ARM64

lazyimp-0.0.6-cp312-cp312-macosx_15_0_arm64.whl (18.5 kB view details)

Uploaded CPython 3.12macOS 15.0+ ARM64

lazyimp-0.0.6-cp311-cp311-macosx_15_0_arm64.whl (18.6 kB view details)

Uploaded CPython 3.11macOS 15.0+ ARM64

File details

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

File metadata

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

File hashes

Hashes for lazyimp-0.0.6.tar.gz
Algorithm Hash digest
SHA256 e523105186574a13b72749c25172a4850051c15e1d5c8daaf6117aafd904567f
MD5 a5d52c707e8273ba18a4cb5ba7172cdd
BLAKE2b-256 471cb67c347a8bf1194dc834d939c5fb9c1f5a7e3242dad4fec153f8387d4fdb

See more details on using hashes here.

File details

Details for the file lazyimp-0.0.6-cp314-cp314-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for lazyimp-0.0.6-cp314-cp314-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 ff71924ecf5f9c99d1deeb2fc2e2643b0e297a72a958846354054f5980a16a27
MD5 3e1ec0c49f31688d85a2fdaf958212e9
BLAKE2b-256 a82c8819a7b804435e11c85da16839572ccdf21116ea49f1490b00d25d4f0a65

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for lazyimp-0.0.6-cp313-cp313-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 004594e22e4fed70acfaa3c6a9b81638a0209f29280a98f660392166332a732d
MD5 d71535562baa4afc96c082b743f1c9a9
BLAKE2b-256 2a90a26a01d897178e4e6477da96049742401f5b9d7408d00de1250a58b685ef

See more details on using hashes here.

File details

Details for the file lazyimp-0.0.6-cp312-cp312-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for lazyimp-0.0.6-cp312-cp312-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 d714bc8572aa977dc26dcdcace261a99220dbe4039d771e2d58e812d63388088
MD5 2fde7883bae13c3c04b91c0a54cf851f
BLAKE2b-256 cabb90940475933b85eb569c31e0ac9eb148f3019d3958b1abe65ea186b93a12

See more details on using hashes here.

File details

Details for the file lazyimp-0.0.6-cp311-cp311-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for lazyimp-0.0.6-cp311-cp311-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 51ed1c31178fb0c39da66d335cbeaff78411ea0a82145292c3ec17db76d65a48
MD5 c9597cd469811b35e3618a0526b0d13a
BLAKE2b-256 d8b8dd64acff4e40a85b8b3da3d259affa8ead04e64573a09a9a96f34066a71c

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