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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file lazyimp-0.0.2.tar.gz.
File metadata
- Download URL: lazyimp-0.0.2.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5591c86138817d5f21ddd05f66474e871f1e9e623d501b081c9055c1c9eba3e3
|
|
| MD5 |
cc8f40883d0b75e6ca601063ccea9218
|
|
| BLAKE2b-256 |
d7eb54d8942213cca2bdfa5dd1e7ecb9971aa875124cbb3c22bdbe0e5b64bf03
|
File details
Details for the file lazyimp-0.0.2-cp313-cp313-macosx_15_0_arm64.whl.
File metadata
- Download URL: lazyimp-0.0.2-cp313-cp313-macosx_15_0_arm64.whl
- Upload date:
- Size: 15.2 kB
- Tags: CPython 3.13, macOS 15.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0471d69d3a5f2f146c300764b3d5446b00d472252a8b92453dfe9d4ce51e71c8
|
|
| MD5 |
088b4507550d2f06f943763f41664e42
|
|
| BLAKE2b-256 |
c7aefce93836a3d8b5cf3cf4628365e9bf43713284a063fbce7f5216ec1b66ca
|