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.5.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.5-cp314-cp314-macosx_15_0_arm64.whl (18.5 kB view details)

Uploaded CPython 3.14macOS 15.0+ ARM64

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

Uploaded CPython 3.13macOS 15.0+ ARM64

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

Uploaded CPython 3.12macOS 15.0+ ARM64

lazyimp-0.0.5-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.5.tar.gz.

File metadata

  • Download URL: lazyimp-0.0.5.tar.gz
  • Upload date:
  • Size: 16.0 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.5.tar.gz
Algorithm Hash digest
SHA256 86ba851d279e7e316e03ec2d89fb9a5ea0526023de6be0b8f7595c058f61a5ff
MD5 662ecc5357c05d06407e908326553cde
BLAKE2b-256 f2acd5abc3b3a90b6aa2d2d96c2db07fd2c22c72bd96cfcec62c39c6f800170b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for lazyimp-0.0.5-cp314-cp314-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 f2e10fb2068ec9150a38d3af2a1cce9eef4d9365ba4f3f9d232b83a66accc687
MD5 bdacade517e2bd751a462fa6d43e20f3
BLAKE2b-256 18395ce589257b48d85623401e5254dcbbbfa2655995c03f78e7845400ab15c0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for lazyimp-0.0.5-cp313-cp313-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 f6cb0b0f399fda727b055d51739b3082bb27221c64af96f6e9d0c0bd69aca208
MD5 7cc64433c8375846f4d5a9e8df7eede8
BLAKE2b-256 07e49f7fba5aa46b2e420f4234ee40c25857b33140a54224a8503071272afca1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for lazyimp-0.0.5-cp312-cp312-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 a025b27a5e024c67c17abcbb8862fc3eaaa7e35d1b96bc8b33a6bbb9fb4cf401
MD5 88ec03e935173f6cb3254aff8cf8e7df
BLAKE2b-256 871c0a366d19357d6705dd4387393bc2826088a5b3e44370883a14424af7bc2f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for lazyimp-0.0.5-cp311-cp311-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 8759480f1215fddbd6a6db771870d0ab56e383116df5d0f03b9b9e8dccc1540d
MD5 d0b1bbe327cd893527f8d02ad1d79295
BLAKE2b-256 04d708628f5f72b4485168bf6e43b01b2ea8322f058d0317ec11e1f106e7824b

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