From 6ea407fa19f404f0e1d3afa1bd0272a4df0de40a Mon Sep 17 00:00:00 2001 From: Tamir Duberstein Date: Thu, 23 Jul 2026 14:08:38 -0400 Subject: [PATCH] python: simplify lazy-import type-checking guards --- .../src/main/resources/python/__init__api.mustache | 4 +++- .../src/main/resources/python/__init__model.mustache | 4 +++- .../src/main/resources/python/__init__package.mustache | 4 +++- .../petstore/python-lazyImports/petstore_api/__init__.py | 4 +++- .../petstore/python-lazyImports/petstore_api/api/__init__.py | 4 +++- .../python-lazyImports/petstore_api/models/__init__.py | 4 +++- 6 files changed, 18 insertions(+), 6 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/python/__init__api.mustache b/modules/openapi-generator/src/main/resources/python/__init__api.mustache index dff26fb1fce3..250aec3c97ea 100644 --- a/modules/openapi-generator/src/main/resources/python/__init__api.mustache +++ b/modules/openapi-generator/src/main/resources/python/__init__api.mustache @@ -3,7 +3,9 @@ {{^lazyImports}} {{>exports_api}} {{/lazyImports}} -{{#lazyImports}}if __import__("typing").TYPE_CHECKING: +{{#lazyImports}}import typing as _typing + +if _typing.TYPE_CHECKING: {{>exports_api}} else: from lazy_imports import LazyModule, as_package, load diff --git a/modules/openapi-generator/src/main/resources/python/__init__model.mustache b/modules/openapi-generator/src/main/resources/python/__init__model.mustache index 117f69132c40..ea5dd406d5e7 100644 --- a/modules/openapi-generator/src/main/resources/python/__init__model.mustache +++ b/modules/openapi-generator/src/main/resources/python/__init__model.mustache @@ -6,7 +6,9 @@ {{^lazyImports}} {{>exports_model}} {{/lazyImports}} -{{#lazyImports}}if __import__("typing").TYPE_CHECKING: +{{#lazyImports}}import typing as _typing + +if _typing.TYPE_CHECKING: {{>exports_model}} else: from lazy_imports import LazyModule, as_package, load diff --git a/modules/openapi-generator/src/main/resources/python/__init__package.mustache b/modules/openapi-generator/src/main/resources/python/__init__package.mustache index 73f0adf0d473..e38711c252fc 100644 --- a/modules/openapi-generator/src/main/resources/python/__init__package.mustache +++ b/modules/openapi-generator/src/main/resources/python/__init__package.mustache @@ -27,7 +27,9 @@ __all__ = [ {{^lazyImports}} {{>exports_package}} {{/lazyImports}} -{{#lazyImports}}if __import__("typing").TYPE_CHECKING: +{{#lazyImports}}import typing as _typing + +if _typing.TYPE_CHECKING: {{>exports_package}} else: from lazy_imports import LazyModule, as_package, load diff --git a/samples/openapi3/client/petstore/python-lazyImports/petstore_api/__init__.py b/samples/openapi3/client/petstore/python-lazyImports/petstore_api/__init__.py index 64ad7b567d1a..0acc6fb092e8 100644 --- a/samples/openapi3/client/petstore/python-lazyImports/petstore_api/__init__.py +++ b/samples/openapi3/client/petstore/python-lazyImports/petstore_api/__init__.py @@ -152,7 +152,9 @@ "WithNestedOneOf", ] -if __import__("typing").TYPE_CHECKING: +import typing as _typing + +if _typing.TYPE_CHECKING: # import apis into sdk package from petstore_api.api.another_fake_api import AnotherFakeApi as AnotherFakeApi from petstore_api.api.default_api import DefaultApi as DefaultApi diff --git a/samples/openapi3/client/petstore/python-lazyImports/petstore_api/api/__init__.py b/samples/openapi3/client/petstore/python-lazyImports/petstore_api/api/__init__.py index e77aed00eb57..2f99d2cac4f7 100644 --- a/samples/openapi3/client/petstore/python-lazyImports/petstore_api/api/__init__.py +++ b/samples/openapi3/client/petstore/python-lazyImports/petstore_api/api/__init__.py @@ -1,6 +1,8 @@ # flake8: noqa -if __import__("typing").TYPE_CHECKING: +import typing as _typing + +if _typing.TYPE_CHECKING: # import apis into api package from petstore_api.api.another_fake_api import AnotherFakeApi from petstore_api.api.default_api import DefaultApi diff --git a/samples/openapi3/client/petstore/python-lazyImports/petstore_api/models/__init__.py b/samples/openapi3/client/petstore/python-lazyImports/petstore_api/models/__init__.py index 9f052c816cb7..1b3694dc945b 100644 --- a/samples/openapi3/client/petstore/python-lazyImports/petstore_api/models/__init__.py +++ b/samples/openapi3/client/petstore/python-lazyImports/petstore_api/models/__init__.py @@ -12,7 +12,9 @@ Do not edit the class manually. """ # noqa: E501 -if __import__("typing").TYPE_CHECKING: +import typing as _typing + +if _typing.TYPE_CHECKING: # import models into model package from petstore_api.models.additional_properties_any_type import AdditionalPropertiesAnyType from petstore_api.models.additional_properties_class import AdditionalPropertiesClass