Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.9/dist-packages/networkx/lazy_imports.py: 22%
54 statements
« prev ^ index » next coverage.py v7.3.2, created at 2023-10-20 07:00 +0000
« prev ^ index » next coverage.py v7.3.2, created at 2023-10-20 07:00 +0000
1import importlib
2import importlib.util
3import inspect
4import os
5import sys
6import types
8__all__ = ["attach", "_lazy_import"]
11def attach(module_name, submodules=None, submod_attrs=None):
12 """Attach lazily loaded submodules, and functions or other attributes.
14 Typically, modules import submodules and attributes as follows::
16 import mysubmodule
17 import anothersubmodule
19 from .foo import someattr
21 The idea of this function is to replace the `__init__.py`
22 module's `__getattr__`, `__dir__`, and `__all__` attributes such that
23 all imports work exactly the way they normally would, except that the
24 actual import is delayed until the resulting module object is first used.
26 The typical way to call this function, replacing the above imports, is::
28 __getattr__, __lazy_dir__, __all__ = lazy.attach(
29 __name__,
30 ['mysubmodule', 'anothersubmodule'],
31 {'foo': 'someattr'}
32 )
34 This functionality requires Python 3.7 or higher.
36 Parameters
37 ----------
38 module_name : str
39 Typically use __name__.
40 submodules : set
41 List of submodules to lazily import.
42 submod_attrs : dict
43 Dictionary of submodule -> list of attributes / functions.
44 These attributes are imported as they are used.
46 Returns
47 -------
48 __getattr__, __dir__, __all__
50 """
51 if submod_attrs is None:
52 submod_attrs = {}
54 if submodules is None:
55 submodules = set()
56 else:
57 submodules = set(submodules)
59 attr_to_modules = {
60 attr: mod for mod, attrs in submod_attrs.items() for attr in attrs
61 }
63 __all__ = list(submodules | attr_to_modules.keys())
65 def __getattr__(name):
66 if name in submodules:
67 return importlib.import_module(f"{module_name}.{name}")
68 elif name in attr_to_modules:
69 submod = importlib.import_module(f"{module_name}.{attr_to_modules[name]}")
70 return getattr(submod, name)
71 else:
72 raise AttributeError(f"No {module_name} attribute {name}")
74 def __dir__():
75 return __all__
77 if os.environ.get("EAGER_IMPORT", ""):
78 for attr in set(attr_to_modules.keys()) | submodules:
79 __getattr__(attr)
81 return __getattr__, __dir__, list(__all__)
84class DelayedImportErrorModule(types.ModuleType):
85 def __init__(self, frame_data, *args, **kwargs):
86 self.__frame_data = frame_data
87 super().__init__(*args, **kwargs)
89 def __getattr__(self, x):
90 if x in ("__class__", "__file__", "__frame_data"):
91 super().__getattr__(x)
92 else:
93 fd = self.__frame_data
94 raise ModuleNotFoundError(
95 f"No module named '{fd['spec']}'\n\n"
96 "This error is lazily reported, having originally occurred in\n"
97 f' File {fd["filename"]}, line {fd["lineno"]}, in {fd["function"]}\n\n'
98 f'----> {"".join(fd["code_context"]).strip()}'
99 )
102def _lazy_import(fullname):
103 """Return a lazily imported proxy for a module or library.
105 Warning
106 -------
107 Importing using this function can currently cause trouble
108 when the user tries to import from a subpackage of a module before
109 the package is fully imported. In particular, this idiom may not work:
111 np = lazy_import("numpy")
112 from numpy.lib import recfunctions
114 This is due to a difference in the way Python's LazyLoader handles
115 subpackage imports compared to the normal import process. Hopefully
116 we will get Python's LazyLoader to fix this, or find a workaround.
117 In the meantime, this is a potential problem.
119 The workaround is to import numpy before importing from the subpackage.
121 Notes
122 -----
123 We often see the following pattern::
125 def myfunc():
126 import scipy as sp
127 sp.argmin(...)
128 ....
130 This is to prevent a library, in this case `scipy`, from being
131 imported at function definition time, since that can be slow.
133 This function provides a proxy module that, upon access, imports
134 the actual module. So the idiom equivalent to the above example is::
136 sp = lazy.load("scipy")
138 def myfunc():
139 sp.argmin(...)
140 ....
142 The initial import time is fast because the actual import is delayed
143 until the first attribute is requested. The overall import time may
144 decrease as well for users that don't make use of large portions
145 of the library.
147 Parameters
148 ----------
149 fullname : str
150 The full name of the package or subpackage to import. For example::
152 sp = lazy.load('scipy') # import scipy as sp
153 spla = lazy.load('scipy.linalg') # import scipy.linalg as spla
155 Returns
156 -------
157 pm : importlib.util._LazyModule
158 Proxy module. Can be used like any regularly imported module.
159 Actual loading of the module occurs upon first attribute request.
161 """
162 try:
163 return sys.modules[fullname]
164 except:
165 pass
167 # Not previously loaded -- look it up
168 spec = importlib.util.find_spec(fullname)
170 if spec is None:
171 try:
172 parent = inspect.stack()[1]
173 frame_data = {
174 "spec": fullname,
175 "filename": parent.filename,
176 "lineno": parent.lineno,
177 "function": parent.function,
178 "code_context": parent.code_context,
179 }
180 return DelayedImportErrorModule(frame_data, "DelayedImportErrorModule")
181 finally:
182 del parent
184 module = importlib.util.module_from_spec(spec)
185 sys.modules[fullname] = module
187 loader = importlib.util.LazyLoader(spec.loader)
188 loader.exec_module(module)
190 return module