Repository navigation
Expand file tree
/
Copy pathimport_.py
More file actions
172 lines (144 loc) · 6.16 KB
/
Copy pathimport_.py
File metadata and controls
172 lines (144 loc) · 6.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
"""
This module provides utilities for importing modules and handling exceptions.
Classes:
DummyError(Exception):
A custom exception class used as a default for exception handling.
Functions:
import_global(name, modules=None, exceptions=DummyError, locals_=None,
globals_=None, level=-1):
Imports the requested items into the global scope, with support for
relative imports and custom exception handling.
"""
import importlib
import types
import typing
from python_utils import _aliases
class DummyError(Exception):
"""A custom exception class used as a default for exception handling."""
#: Backwards-compatible legacy alias for ``DummyError``.
DummyException = DummyError
def _get_attribute(module: typing.Any, attr: str, name: str) -> typing.Any:
"""Return ``module.attr``, importing it as a submodule when needed.
A submodule only becomes an attribute of its parent once something has
imported it.
Args:
module: The module, or other object, to take the attribute from.
attr: The name of the attribute or submodule.
name: The full dotted name that is being imported, for the error.
Returns:
The attribute or the imported submodule.
Raises:
ImportError: When ``module`` has no such attribute or submodule. An
error that the submodule raises while it is imported is passed
on as it is, a missing dependency included.
"""
try:
return getattr(module, attr)
except AttributeError as error:
if not isinstance(module, types.ModuleType):
# The same error as for a missing module, as it always was.
raise ImportError( # noqa: TRY004
f'No module named {name}'
) from error
submodule: str = f'{module.__name__}.{attr}'
try:
return importlib.import_module(submodule)
except ModuleNotFoundError as error:
missing: str = error.name or ''
if missing != submodule and not submodule.startswith(f'{missing}.'):
# The submodule exists and one of its own imports is missing.
raise
raise ImportError(f'No module named {name}') from error
def import_global( # noqa: C901
name: str,
modules: list[str] | None = None,
exceptions: _aliases.ExceptionsType = DummyError,
locals_: _aliases.OptionalScope = None,
globals_: _aliases.OptionalScope = None,
level: int = -1,
) -> typing.Any: # sourcery skip: hoist-if-from-if
"""Import the requested items into the global scope.
WARNING! this method _will_ overwrite your global scope
If you have a variable named `path` and you call `import_global('sys')`
it will be overwritten with `sys.path`
Args:
name (str): the name of the module to import, e.g. sys
modules (list[str]): the names to import from the module, use None
for everything. An empty list also imports everything. A single
name needs a list as well, a bare string is read as a collection
of one-character names. Names that start with an underscore are
never imported. Declared or explicitly requested names may be
provided lazily by the module, even if absent from ``dir()``.
exceptions (Exception): the exception to catch, e.g. ImportError
locals_: the `locals()` method (in case you need a different scope)
globals_: the `globals()` method (in case you need a different scope)
level (int): the level to import from, this can be used for
relative imports
"""
frame = None
name_parts: list[str] = name.split('.')
modules_set: set[object] = set()
try:
# If locals_ or globals_ are not given, autodetect them by inspecting
# the current stack
if locals_ is None or globals_ is None:
import inspect
frame = inspect.stack()[1][0]
if locals_ is None:
locals_ = frame.f_locals
if globals_ is None:
globals_ = frame.f_globals
try:
# Relative imports are supported (from .spam import eggs)
if not name_parts[0]:
name_parts = name_parts[1:]
level = 1
# raise IOError((name, level))
module = __import__(
name=name_parts[0] or '.',
globals=globals_,
locals=locals_,
fromlist=name_parts[1:],
level=max(level, 0),
)
# Make sure we get the right part of a dotted import (i.e.
# spam.eggs should return eggs, not spam)
for attr in name_parts[1:]:
module = _get_attribute(module, attr, '.'.join(name_parts))
# If no list of modules is given, autodetect from either __all__
# or a dir() of the module
if not modules:
modules_set = set(getattr(module, '__all__', dir(module)))
else:
modules_set = set(modules)
# Add all items in modules to the global scope. A name that dir()
# does not list can still be a lazy export of the module's
# __getattr__. It is resolved only when __all__ declares it, and
# skipped when that fails, as it was before lazy exports.
listed = set(dir(module))
exported: set[object] = set(getattr(module, '__all__', ()))
for k in modules_set:
if not isinstance(k, str) or not k or k[0] == '_':
continue
if k in listed:
globals_[k] = getattr(module, k)
elif k in exported:
try:
globals_[k] = getattr(module, k)
except Exception:
# A failing lazy export was never imported before.
continue
except exceptions as e:
return e
finally:
# Clean up, just to be sure
del ( # pyrefly: ignore[unsupported-delete]
name,
name_parts,
modules,
modules_set,
exceptions,
locals_,
globals_,
frame,
)