Skip to content

Commit 9182ac7

Browse files
committed
python: reinstate lazy imports
1 parent ad1bbdb commit 9182ac7

9 files changed

Lines changed: 79 additions & 33 deletions

File tree

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
# flake8: noqa
22

3-
# import apis into api package
4-
{{#apiInfo}}{{#apis}}from {{apiPackage}}.{{classFilename}} import {{classname}}
5-
{{/apis}}{{/apiInfo}}
3+
from typing import TYPE_CHECKING
4+
5+
from lazy_imports import LazyModule, as_package, load
6+
7+
8+
if TYPE_CHECKING:
9+
{{>exports_api}}
10+
else:
11+
load(
12+
LazyModule(
13+
*as_package(__file__),
14+
"""{{>exports_api}}""",
15+
name=__name__,
16+
doc=__doc__,
17+
)
18+
)

modules/openapi-generator/src/main/resources/python/__init__model.mustache

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,19 @@
33
# flake8: noqa
44
{{>partial_header}}
55

6+
from typing import TYPE_CHECKING
67

7-
# import models into model package
8-
{{#models}}
9-
{{#model}}
10-
from {{modelPackage}}.{{classFilename}} import {{classname}}
11-
{{/model}}
12-
{{/models}}
8+
from lazy_imports import LazyModule, as_package, load
9+
10+
11+
if TYPE_CHECKING:
12+
{{>exports_model}}
13+
else:
14+
load(
15+
LazyModule(
16+
*as_package(__file__),
17+
"""{{>exports_model}}""",
18+
name=__name__,
19+
doc=__doc__,
20+
)
21+
)

modules/openapi-generator/src/main/resources/python/__init__package.mustache

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
{{>partial_header}}
66

7+
from typing import TYPE_CHECKING
8+
9+
from lazy_imports import LazyModule, as_package, load
10+
711

812
__version__ = "{{packageVersion}}"
913

@@ -24,29 +28,19 @@ __all__ = [
2428
{{/-last}}{{#-last}},{{/-last}}{{/model}}{{/models}}
2529
]
2630

27-
# import apis into sdk package
28-
{{#apiInfo}}{{#apis}}from {{apiPackage}}.{{classFilename}} import {{classname}} as {{classname}}
29-
{{/apis}}{{/apiInfo}}
30-
# import ApiClient
31-
from {{packageName}}.api_response import ApiResponse as ApiResponse
32-
from {{packageName}}.api_client import ApiClient as ApiClient
33-
from {{packageName}}.configuration import Configuration as Configuration
34-
from {{packageName}}.exceptions import OpenApiException as OpenApiException
35-
from {{packageName}}.exceptions import ApiTypeError as ApiTypeError
36-
from {{packageName}}.exceptions import ApiValueError as ApiValueError
37-
from {{packageName}}.exceptions import ApiKeyError as ApiKeyError
38-
from {{packageName}}.exceptions import ApiAttributeError as ApiAttributeError
39-
from {{packageName}}.exceptions import ApiException as ApiException
40-
{{#hasHttpSignatureMethods}}
41-
from {{packageName}}.signing import HttpSigningConfiguration as HttpSigningConfiguration
42-
{{/hasHttpSignatureMethods}}
43-
44-
# import models into sdk package
45-
{{#models}}
46-
{{#model}}
47-
from {{modelPackage}}.{{classFilename}} import {{classname}} as {{classname}}
48-
{{/model}}
49-
{{/models}}
31+
if TYPE_CHECKING:
32+
{{>exports_package}}
33+
else:
34+
load(
35+
LazyModule(
36+
*as_package(__file__),
37+
("__version__", __version__),
38+
("__all__", __all__),
39+
"""{{>exports_package}}""",
40+
name=__name__,
41+
doc=__doc__,
42+
)
43+
)
5044
{{#recursionLimit}}
5145

5246
__import__('sys').setrecursionlimit({{{.}}})
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# import apis into api package
2+
{{#apiInfo}}{{#apis}}from {{apiPackage}}.{{classFilename}} import {{classname}}
3+
{{/apis}}{{/apiInfo}}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# import models into model package
2+
{{#models}}{{#model}}from {{modelPackage}}.{{classFilename}} import {{classname}}
3+
{{/model}}{{/models}}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# import apis into sdk package
2+
{{#apiInfo}}{{#apis}}from {{apiPackage}}.{{classFilename}} import {{classname}} as {{classname}}
3+
{{/apis}}{{/apiInfo}}
4+
# import ApiClient
5+
from {{packageName}}.api_response import ApiResponse as ApiResponse
6+
from {{packageName}}.api_client import ApiClient as ApiClient
7+
from {{packageName}}.configuration import Configuration as Configuration
8+
from {{packageName}}.exceptions import OpenApiException as OpenApiException
9+
from {{packageName}}.exceptions import ApiTypeError as ApiTypeError
10+
from {{packageName}}.exceptions import ApiValueError as ApiValueError
11+
from {{packageName}}.exceptions import ApiKeyError as ApiKeyError
12+
from {{packageName}}.exceptions import ApiAttributeError as ApiAttributeError
13+
from {{packageName}}.exceptions import ApiException as ApiException
14+
{{#hasHttpSignatureMethods}}
15+
from {{packageName}}.signing import HttpSigningConfiguration as HttpSigningConfiguration
16+
{{/hasHttpSignatureMethods}}
17+
18+
# import models into sdk package
19+
{{#models}}{{#model}}from {{modelPackage}}.{{classFilename}} import {{classname}} as {{classname}}
20+
{{/model}}{{/models}}

modules/openapi-generator/src/main/resources/python/pyproject.mustache

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ pycryptodome = ">= 3.9.0"
4141
{{/hasHttpSignatureMethods}}
4242
pydantic = ">= 2"
4343
typing-extensions = ">= 4.7.1"
44+
lazy-imports = ">= 1, < 2"
4445
{{/poetry1}}
4546
{{^poetry1}}
4647
requires-python = "^3.9"
@@ -60,7 +61,8 @@ dependencies = [
6061
"pycryptodome (>=3.9.0)",
6162
{{/hasHttpSignatureMethods}}
6263
"pydantic (>=2)",
63-
"typing-extensions (>=4.7.1)"
64+
"typing-extensions (>=4.7.1)",
65+
"lazy-imports (>=1,<2)"
6466
]
6567
6668
[project.urls]

modules/openapi-generator/src/main/resources/python/requirements.mustache

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ pycryptodome >= 3.9.0
1313
{{/hasHttpSignatureMethods}}
1414
pydantic >= 2
1515
typing-extensions >= 4.7.1
16+
lazy-imports >= 1, < 2

modules/openapi-generator/src/main/resources/python/setup.mustache

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ REQUIRES = [
3030
{{/hasHttpSignatureMethods}}
3131
"pydantic >= 2",
3232
"typing-extensions >= 4.7.1",
33+
"lazy-imports >= 1, < 2",
3334
]
3435

3536
setup(

0 commit comments

Comments
 (0)