Skip to content

Commit 0aea5f7

Browse files
bachorpwing328
authored andcommitted
python: feature flag for lazy imports (OpenAPITools#21885)
* python: feature flag for lazy imports * python: update samples * python: add python-lazyImports to test job * python: reuse tests in lazyImports sample * python: avoid using non-imported submodules * add normalizer option --------- Co-authored-by: Pascal Bachor <[email protected]> Co-authored-by: William Cheng <[email protected]>
1 parent ba1193a commit 0aea5f7

File tree

440 files changed

+50287
-818
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

440 files changed

+50287
-818
lines changed

.github/workflows/samples-python-petstore.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ jobs:
3535
sample:
3636
- samples/openapi3/client/petstore/python-aiohttp
3737
- samples/openapi3/client/petstore/python
38+
- samples/openapi3/client/petstore/python-lazyImports
3839
services:
3940
petstore-api:
4041
image: swaggerapi/petstore
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
generatorName: python
2+
outputDir: samples/openapi3/client/petstore/python-lazyImports
3+
inputSpec: modules/openapi-generator/src/test/resources/3_0/python/petstore-with-fake-endpoints-models-for-testing.yaml
4+
templateDir: modules/openapi-generator/src/main/resources/python
5+
additionalProperties:
6+
packageName: petstore_api
7+
useOneOfDiscriminatorLookup: "true"
8+
disallowAdditionalPropertiesIfNotPresent: false
9+
mapNumberTo: StrictFloat
10+
lazyImports: 'true'
11+
nameMappings:
12+
_type: underscore_type
13+
type_: type_with_underscore
14+
modelNameMappings:
15+
# The OpenAPI spec ApiResponse conflicts with the internal ApiResponse
16+
ApiResponse: ModelApiResponse
17+
openapiNormalizer:
18+
SIMPLIFY_ONEOF_ANYOF_ENUM: false

docs/generators/python.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
2424
|disallowAdditionalPropertiesIfNotPresent|If false, the 'additionalProperties' implementation (set to true by default) is compliant with the OAS and JSON schema specifications. If true (default), keep the old (incorrect) behaviour that 'additionalProperties' is set to false by default.|<dl><dt>**false**</dt><dd>The 'additionalProperties' implementation is compliant with the OAS and JSON schema specifications.</dd><dt>**true**</dt><dd>Keep the old (incorrect) behaviour that 'additionalProperties' is set to false by default.</dd></dl>|true|
2525
|generateSourceCodeOnly|Specifies that only a library source code is to be generated.| |false|
2626
|hideGenerationTimestamp|Hides the generation timestamp when files are generated.| |true|
27+
|lazyImports|Enable lazy imports.| |false|
2728
|library|library template (sub-template) to use: asyncio, tornado (deprecated), urllib3| |urllib3|
2829
|mapNumberTo|Map number to Union[StrictFloat, StrictInt], StrictStr or float.| |Union[StrictFloat, StrictInt]|
2930
|packageName|python package name (convention: snake_case).| |openapi_client|

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonClientCodegen.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public class PythonClientCodegen extends AbstractPythonCodegen implements Codege
4848
public static final String DATE_FORMAT = "dateFormat";
4949
public static final String SET_ENSURE_ASCII_TO_FALSE = "setEnsureAsciiToFalse";
5050
public static final String POETRY1_FALLBACK = "poetry1";
51+
public static final String LAZY_IMPORTS = "lazyImports";
5152

5253
@Setter protected String packageUrl;
5354
protected String apiDocPath = "docs/";
@@ -151,6 +152,7 @@ public PythonClientCodegen() {
151152
.defaultValue("%Y-%m-%d"));
152153
cliOptions.add(new CliOption(CodegenConstants.USE_ONEOF_DISCRIMINATOR_LOOKUP, CodegenConstants.USE_ONEOF_DISCRIMINATOR_LOOKUP_DESC).defaultValue("false"));
153154
cliOptions.add(new CliOption(POETRY1_FALLBACK, "Fallback to formatting pyproject.toml to Poetry 1.x format."));
155+
cliOptions.add(new CliOption(LAZY_IMPORTS, "Enable lazy imports.").defaultValue(Boolean.FALSE.toString()));
154156

155157
supportedLibraries.put("urllib3", "urllib3-based client");
156158
supportedLibraries.put("asyncio", "asyncio-based client");
@@ -264,6 +266,10 @@ public void processOpts() {
264266
additionalProperties.put(DATE_FORMAT, dateFormat);
265267
}
266268

269+
if (additionalProperties.containsKey(LAZY_IMPORTS)) {
270+
additionalProperties.put(LAZY_IMPORTS, Boolean.valueOf(additionalProperties.get(LAZY_IMPORTS).toString()));
271+
}
272+
267273
String modelPath = packagePath() + File.separatorChar + modelPackage.replace('.', File.separatorChar);
268274
String apiPath = packagePath() + File.separatorChar + apiPackage.replace('.', File.separatorChar);
269275

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# flake8: noqa
22

3-
if __import__("typing").TYPE_CHECKING:
3+
{{^lazyImports}}
4+
{{>exports_api}}
5+
{{/lazyImports}}
6+
{{#lazyImports}}if __import__("typing").TYPE_CHECKING:
47
{{>exports_api}}
58
else:
69
from lazy_imports import LazyModule, as_package, load
@@ -13,3 +16,4 @@ else:
1316
doc=__doc__,
1417
)
1518
)
19+
{{/lazyImports}}

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

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

6-
7-
if __import__("typing").TYPE_CHECKING:
6+
{{^lazyImports}}
7+
{{>exports_model}}
8+
{{/lazyImports}}
9+
{{#lazyImports}}if __import__("typing").TYPE_CHECKING:
810
{{>exports_model}}
911
else:
1012
from lazy_imports import LazyModule, as_package, load
@@ -17,3 +19,4 @@ else:
1719
doc=__doc__,
1820
)
1921
)
22+
{{/lazyImports}}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ __all__ = [
2424
{{/-last}}{{#-last}},{{/-last}}{{/model}}{{/models}}
2525
]
2626

27-
if __import__("typing").TYPE_CHECKING:
27+
{{^lazyImports}}
28+
{{>exports_package}}
29+
{{/lazyImports}}
30+
{{#lazyImports}}if __import__("typing").TYPE_CHECKING:
2831
{{>exports_package}}
2932
else:
3033
from lazy_imports import LazyModule, as_package, load
@@ -39,6 +42,7 @@ else:
3942
doc=__doc__,
4043
)
4144
)
45+
{{/lazyImports}}
4246
{{#recursionLimit}}
4347

4448
__import__('sys').setrecursionlimit({{{.}}})

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ pycryptodome = ">= 3.9.0"
4848
{{/hasHttpSignatureMethods}}
4949
pydantic = ">= 2"
5050
typing-extensions = ">= 4.7.1"
51+
{{#lazyImports}}
5152
lazy-imports = ">= 1, < 2"
53+
{{/lazyImports}}
5254
{{/poetry1}}
5355
{{^poetry1}}
5456
requires-python = ">=3.9"
@@ -69,7 +71,9 @@ dependencies = [
6971
{{/hasHttpSignatureMethods}}
7072
"pydantic (>=2)",
7173
"typing-extensions (>=4.7.1)",
74+
{{#lazyImports}}
7275
"lazy-imports (>=1,<2)"
76+
{{/lazyImports}}
7377
]
7478
7579
[project.urls]

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

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

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

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

3638
setup(

0 commit comments

Comments
 (0)