Commit 6088919
authored
fix: sibling module dependencies are eagerly loaded even if only for type annotations (aws#5172)
## Problem
Every generated `init.py` file has import statements at the top that look like this, for example
```
from .aws_iam import IGrantable as _IGrantable_ef567890
from .aws_kms import IKey as _IKey_abc12345
...
```
and cross-module imports that could look like this. for example
```
import scope.jsii_calc_base
...
```
These load immediately whenever each module loads, which in turn load their own dependencies. This causes a huge cascade of imports at module load time. A large chunk of these imports only exist to satisfy type annotations however, or runtime type checks, and therefore this creates a need for optimization.
## Solution
Cross-module imports are now deferred using a `typing.TYPE_CHECKING` split plus a lazy import proxy. Instead of importing each symbol eagerly, the generator emits:
```python
class _LazyImport:
def __init__(self, module_name: str) -> None:
self._module_name = module_name
self._module: typing.Any = None
def __getattr__(self, name: str) -> typing.Any:
if self._module is None:
import importlib
self._module = importlib.import_module(self._module_name)
return getattr(self._module, name)
if typing.TYPE_CHECKING:
import jsii_calc.composition as _composition_4f38e801
import scope.jsii_calc_lib as _scope_jsii_calc_lib_c61f082f
else:
_composition_4f38e801 = _LazyImport("jsii_calc.composition")
_scope_jsii_calc_lib_c61f082f = _LazyImport("scope.jsii_calc_lib")
```
mypy and pyright evaluate the if statement, so they see the real module imports and resolve all types normally. At runtime, the `else:` branch executes instead, binding each module alias to a `_LazyImport` proxy. The actual `importlib.import_module()` call is deferred until the first attribute access through the proxy's `__getattr__`.
---
By submitting this pull request, I confirm that my contribution is made under the terms of the [Apache 2.0 license].
[Apache 2.0 license]: https://www.apache.org/licenses/LICENSE-2.01 parent f3e1928 commit 6088919
4 files changed
Lines changed: 775 additions & 321 deletions
File tree
- packages/jsii-pacmak
- lib/targets
- python
- test
- generated-code/__snapshots__
- targets/python
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
| 28 | + | |
28 | 29 | | |
29 | 30 | | |
30 | 31 | | |
| |||
1777 | 1778 | | |
1778 | 1779 | | |
1779 | 1780 | | |
| 1781 | + | |
| 1782 | + | |
| 1783 | + | |
| 1784 | + | |
| 1785 | + | |
| 1786 | + | |
| 1787 | + | |
| 1788 | + | |
| 1789 | + | |
1780 | 1790 | | |
1781 | 1791 | | |
1782 | 1792 | | |
| |||
2113 | 2123 | | |
2114 | 2124 | | |
2115 | 2125 | | |
| 2126 | + | |
| 2127 | + | |
| 2128 | + | |
| 2129 | + | |
| 2130 | + | |
| 2131 | + | |
| 2132 | + | |
| 2133 | + | |
| 2134 | + | |
| 2135 | + | |
| 2136 | + | |
| 2137 | + | |
| 2138 | + | |
| 2139 | + | |
| 2140 | + | |
| 2141 | + | |
| 2142 | + | |
| 2143 | + | |
| 2144 | + | |
| 2145 | + | |
| 2146 | + | |
| 2147 | + | |
| 2148 | + | |
| 2149 | + | |
| 2150 | + | |
| 2151 | + | |
| 2152 | + | |
| 2153 | + | |
| 2154 | + | |
| 2155 | + | |
| 2156 | + | |
| 2157 | + | |
| 2158 | + | |
| 2159 | + | |
| 2160 | + | |
| 2161 | + | |
| 2162 | + | |
| 2163 | + | |
| 2164 | + | |
| 2165 | + | |
| 2166 | + | |
| 2167 | + | |
| 2168 | + | |
| 2169 | + | |
| 2170 | + | |
| 2171 | + | |
| 2172 | + | |
| 2173 | + | |
| 2174 | + | |
| 2175 | + | |
| 2176 | + | |
| 2177 | + | |
| 2178 | + | |
| 2179 | + | |
| 2180 | + | |
| 2181 | + | |
| 2182 | + | |
| 2183 | + | |
| 2184 | + | |
| 2185 | + | |
| 2186 | + | |
2116 | 2187 | | |
2117 | 2188 | | |
2118 | 2189 | | |
| |||
2142 | 2213 | | |
2143 | 2214 | | |
2144 | 2215 | | |
2145 | | - | |
2146 | | - | |
| 2216 | + | |
| 2217 | + | |
| 2218 | + | |
| 2219 | + | |
| 2220 | + | |
| 2221 | + | |
| 2222 | + | |
| 2223 | + | |
| 2224 | + | |
| 2225 | + | |
| 2226 | + | |
| 2227 | + | |
| 2228 | + | |
| 2229 | + | |
| 2230 | + | |
| 2231 | + | |
| 2232 | + | |
| 2233 | + | |
| 2234 | + | |
| 2235 | + | |
| 2236 | + | |
| 2237 | + | |
2147 | 2238 | | |
2148 | 2239 | | |
2149 | 2240 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
152 | 152 | | |
153 | 153 | | |
154 | 154 | | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
155 | 162 | | |
156 | 163 | | |
157 | 164 | | |
| |||
398 | 405 | | |
399 | 406 | | |
400 | 407 | | |
401 | | - | |
402 | | - | |
| 408 | + | |
403 | 409 | | |
404 | 410 | | |
405 | | - | |
| 411 | + | |
406 | 412 | | |
407 | 413 | | |
408 | | - | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
409 | 417 | | |
410 | | - | |
411 | | - | |
412 | | - | |
413 | | - | |
414 | | - | |
| 418 | + | |
| 419 | + | |
415 | 420 | | |
416 | 421 | | |
417 | 422 | | |
| |||
488 | 493 | | |
489 | 494 | | |
490 | 495 | | |
491 | | - | |
| 496 | + | |
492 | 497 | | |
493 | 498 | | |
494 | 499 | | |
| |||
0 commit comments