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.4.tar.gz (12.7 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.4-cp313-cp313-macosx_15_0_arm64.whl (15.1 kB view details)

Uploaded CPython 3.13macOS 15.0+ ARM64

lazyimp-0.0.4-cp312-cp312-macosx_15_0_arm64.whl (15.1 kB view details)

Uploaded CPython 3.12macOS 15.0+ ARM64

lazyimp-0.0.4-cp311-cp311-macosx_15_0_arm64.whl (15.1 kB view details)

Uploaded CPython 3.11macOS 15.0+ ARM64

File details

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

File metadata

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

File hashes

Hashes for lazyimp-0.0.4.tar.gz
Algorithm Hash digest
SHA256 2f217ca2692362362e963b78b65c6771905a98371ec0dc38f48e250664f14fda
MD5 c169479c9c48c733a96f76c3b959f856
BLAKE2b-256 9cb9968959d1a7ac30fde19740f6c21437e7cc22cbd1e2dbed1a5ea42aa16bc3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for lazyimp-0.0.4-cp313-cp313-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 abcf49cb009ecd200857fc75c05039e82d526e8421e8e28a571cb760b4a83401
MD5 572067757d82aad061625a79bb269e53
BLAKE2b-256 b25a4af1fbb41c4627eb2f706069c012d4c337b50c490ff15338ee17da9bc746

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for lazyimp-0.0.4-cp312-cp312-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 23abf304f32419eb0040f3b26e60bfa625e95568523ff3cd51d2bf9ee31eb97a
MD5 4b948f8778057bfe915495b065697ec0
BLAKE2b-256 69bc822fd8dbc756208d15a4f5cd4e04becb2ce5b6d8c8f7380037763fe4cfe0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for lazyimp-0.0.4-cp311-cp311-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 fedb25195a60c285e9d642932fe7f368a42a81a77fc25598841e7da34e627a42
MD5 8010395fc5dfbab52f8625ec3e6b1d6c
BLAKE2b-256 0ca4b56bd1f207178163a910d4579e4d4da605230e967c8915e750ea88c54aa0

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