diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractPythonCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractPythonCodegen.java index 3c76c7dd170..12dad3e640c 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractPythonCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractPythonCodegen.java @@ -1747,6 +1747,7 @@ public abstract class AbstractPythonCodegen extends DefaultCodegen implements Co @Override public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List allModels) { hasModelsToImport = false; + boolean importAnnotated = false; TreeSet typingImports = new TreeSet<>(); TreeSet pydanticImports = new TreeSet<>(); TreeSet datetimeImports = new TreeSet<>(); @@ -1807,6 +1808,7 @@ public abstract class AbstractPythonCodegen extends DefaultCodegen implements Co param.vendorExtensions.put("x-py-typing", typing); } else { param.vendorExtensions.put("x-py-typing", String.format(Locale.ROOT, "Annotated[%s, %s]", typing, fieldCustomization)); + importAnnotated = true; } } @@ -1838,6 +1840,12 @@ public abstract class AbstractPythonCodegen extends DefaultCodegen implements Co List> newImports = new ArrayList<>(); + if (importAnnotated) { + Map item = new HashMap<>(); + item.put("import", String.format(Locale.ROOT, String.format(Locale.ROOT, "from typing_extensions import Annotated"))); + newImports.add(item); + } + // need datetime import if (!datetimeImports.isEmpty()) { Map item = new HashMap<>(); diff --git a/modules/openapi-generator/src/main/resources/python/api.mustache b/modules/openapi-generator/src/main/resources/python/api.mustache index 70f316a2e7f..740f2b2f20d 100644 --- a/modules/openapi-generator/src/main/resources/python/api.mustache +++ b/modules/openapi-generator/src/main/resources/python/api.mustache @@ -7,8 +7,9 @@ import io import warnings from pydantic import validate_arguments, ValidationError -from typing_extensions import Annotated{{#asyncio}} -from typing import overload, Optional, Union, Awaitable{{/asyncio}} +{{#asyncio}} +from typing import overload, Optional, Union, Awaitable +{{/asyncio}} {{#imports}} {{import}} diff --git a/samples/client/echo_api/python/openapi_client/api/auth_api.py b/samples/client/echo_api/python/openapi_client/api/auth_api.py index 07cdda86f58..16ee7463aad 100644 --- a/samples/client/echo_api/python/openapi_client/api/auth_api.py +++ b/samples/client/echo_api/python/openapi_client/api/auth_api.py @@ -18,7 +18,6 @@ import io import warnings from pydantic import validate_arguments, ValidationError -from typing_extensions import Annotated from openapi_client.api_client import ApiClient diff --git a/samples/client/echo_api/python/openapi_client/api/body_api.py b/samples/client/echo_api/python/openapi_client/api/body_api.py index 196e2a6389f..553feae23a3 100644 --- a/samples/client/echo_api/python/openapi_client/api/body_api.py +++ b/samples/client/echo_api/python/openapi_client/api/body_api.py @@ -18,8 +18,8 @@ import io import warnings from pydantic import validate_arguments, ValidationError -from typing_extensions import Annotated +from typing_extensions import Annotated from pydantic import Field, StrictBytes, StrictStr, conlist from typing import Any, Dict, Optional, Union diff --git a/samples/client/echo_api/python/openapi_client/api/form_api.py b/samples/client/echo_api/python/openapi_client/api/form_api.py index 808dd48ffa2..0071725761d 100644 --- a/samples/client/echo_api/python/openapi_client/api/form_api.py +++ b/samples/client/echo_api/python/openapi_client/api/form_api.py @@ -18,7 +18,6 @@ import io import warnings from pydantic import validate_arguments, ValidationError -from typing_extensions import Annotated from pydantic import StrictBool, StrictInt, StrictStr diff --git a/samples/client/echo_api/python/openapi_client/api/header_api.py b/samples/client/echo_api/python/openapi_client/api/header_api.py index a67fc13b277..7f9dc25354f 100644 --- a/samples/client/echo_api/python/openapi_client/api/header_api.py +++ b/samples/client/echo_api/python/openapi_client/api/header_api.py @@ -18,7 +18,6 @@ import io import warnings from pydantic import validate_arguments, ValidationError -from typing_extensions import Annotated from pydantic import StrictBool, StrictInt, StrictStr diff --git a/samples/client/echo_api/python/openapi_client/api/path_api.py b/samples/client/echo_api/python/openapi_client/api/path_api.py index 13203624883..35d0c6a11b0 100644 --- a/samples/client/echo_api/python/openapi_client/api/path_api.py +++ b/samples/client/echo_api/python/openapi_client/api/path_api.py @@ -18,7 +18,6 @@ import io import warnings from pydantic import validate_arguments, ValidationError -from typing_extensions import Annotated from pydantic import StrictInt, StrictStr diff --git a/samples/client/echo_api/python/openapi_client/api/query_api.py b/samples/client/echo_api/python/openapi_client/api/query_api.py index 59465cdc0cc..09d55efd2f5 100644 --- a/samples/client/echo_api/python/openapi_client/api/query_api.py +++ b/samples/client/echo_api/python/openapi_client/api/query_api.py @@ -18,7 +18,6 @@ import io import warnings from pydantic import validate_arguments, ValidationError -from typing_extensions import Annotated from datetime import date, datetime diff --git a/samples/openapi3/client/petstore/python-aiohttp/petstore_api/api/another_fake_api.py b/samples/openapi3/client/petstore/python-aiohttp/petstore_api/api/another_fake_api.py index 3ba5d212b86..140dcf43460 100644 --- a/samples/openapi3/client/petstore/python-aiohttp/petstore_api/api/another_fake_api.py +++ b/samples/openapi3/client/petstore/python-aiohttp/petstore_api/api/another_fake_api.py @@ -17,9 +17,9 @@ import io import warnings from pydantic import validate_arguments, ValidationError -from typing_extensions import Annotated from typing import overload, Optional, Union, Awaitable +from typing_extensions import Annotated from pydantic import Field from petstore_api.models.client import Client diff --git a/samples/openapi3/client/petstore/python-aiohttp/petstore_api/api/default_api.py b/samples/openapi3/client/petstore/python-aiohttp/petstore_api/api/default_api.py index 69cfa5ebedf..be02dd6b8a7 100644 --- a/samples/openapi3/client/petstore/python-aiohttp/petstore_api/api/default_api.py +++ b/samples/openapi3/client/petstore/python-aiohttp/petstore_api/api/default_api.py @@ -17,7 +17,6 @@ import io import warnings from pydantic import validate_arguments, ValidationError -from typing_extensions import Annotated from typing import overload, Optional, Union, Awaitable from petstore_api.models.foo_get_default_response import FooGetDefaultResponse diff --git a/samples/openapi3/client/petstore/python-aiohttp/petstore_api/api/fake_api.py b/samples/openapi3/client/petstore/python-aiohttp/petstore_api/api/fake_api.py index 201644a2177..3c0ef29b788 100644 --- a/samples/openapi3/client/petstore/python-aiohttp/petstore_api/api/fake_api.py +++ b/samples/openapi3/client/petstore/python-aiohttp/petstore_api/api/fake_api.py @@ -17,9 +17,9 @@ import io import warnings from pydantic import validate_arguments, ValidationError -from typing_extensions import Annotated from typing import overload, Optional, Union, Awaitable +from typing_extensions import Annotated from datetime import date, datetime from pydantic import Field, StrictBool, StrictBytes, StrictInt, StrictStr, conbytes, confloat, conint, conlist, constr, validator diff --git a/samples/openapi3/client/petstore/python-aiohttp/petstore_api/api/fake_classname_tags123_api.py b/samples/openapi3/client/petstore/python-aiohttp/petstore_api/api/fake_classname_tags123_api.py index 1553c2d10b8..6b9c88c1212 100644 --- a/samples/openapi3/client/petstore/python-aiohttp/petstore_api/api/fake_classname_tags123_api.py +++ b/samples/openapi3/client/petstore/python-aiohttp/petstore_api/api/fake_classname_tags123_api.py @@ -17,9 +17,9 @@ import io import warnings from pydantic import validate_arguments, ValidationError -from typing_extensions import Annotated from typing import overload, Optional, Union, Awaitable +from typing_extensions import Annotated from pydantic import Field from petstore_api.models.client import Client diff --git a/samples/openapi3/client/petstore/python-aiohttp/petstore_api/api/pet_api.py b/samples/openapi3/client/petstore/python-aiohttp/petstore_api/api/pet_api.py index 21954fd08a9..72e29e7d042 100644 --- a/samples/openapi3/client/petstore/python-aiohttp/petstore_api/api/pet_api.py +++ b/samples/openapi3/client/petstore/python-aiohttp/petstore_api/api/pet_api.py @@ -17,9 +17,9 @@ import io import warnings from pydantic import validate_arguments, ValidationError -from typing_extensions import Annotated from typing import overload, Optional, Union, Awaitable +from typing_extensions import Annotated from pydantic import Field, StrictBytes, StrictInt, StrictStr, conlist, validator from typing import List, Optional, Union diff --git a/samples/openapi3/client/petstore/python-aiohttp/petstore_api/api/store_api.py b/samples/openapi3/client/petstore/python-aiohttp/petstore_api/api/store_api.py index f1e80d7e27b..e52b31ee5e1 100644 --- a/samples/openapi3/client/petstore/python-aiohttp/petstore_api/api/store_api.py +++ b/samples/openapi3/client/petstore/python-aiohttp/petstore_api/api/store_api.py @@ -17,9 +17,9 @@ import io import warnings from pydantic import validate_arguments, ValidationError -from typing_extensions import Annotated from typing import overload, Optional, Union, Awaitable +from typing_extensions import Annotated from pydantic import Field, StrictStr, conint from typing import Dict diff --git a/samples/openapi3/client/petstore/python-aiohttp/petstore_api/api/user_api.py b/samples/openapi3/client/petstore/python-aiohttp/petstore_api/api/user_api.py index b17529a75a9..3c059d73339 100644 --- a/samples/openapi3/client/petstore/python-aiohttp/petstore_api/api/user_api.py +++ b/samples/openapi3/client/petstore/python-aiohttp/petstore_api/api/user_api.py @@ -17,9 +17,9 @@ import io import warnings from pydantic import validate_arguments, ValidationError -from typing_extensions import Annotated from typing import overload, Optional, Union, Awaitable +from typing_extensions import Annotated from pydantic import Field, StrictStr, conlist from petstore_api.models.user import User diff --git a/samples/openapi3/client/petstore/python/petstore_api/api/another_fake_api.py b/samples/openapi3/client/petstore/python/petstore_api/api/another_fake_api.py index 13a77052ca0..c8ae8d0af13 100755 --- a/samples/openapi3/client/petstore/python/petstore_api/api/another_fake_api.py +++ b/samples/openapi3/client/petstore/python/petstore_api/api/another_fake_api.py @@ -17,8 +17,8 @@ import io import warnings from pydantic import validate_arguments, ValidationError -from typing_extensions import Annotated +from typing_extensions import Annotated from pydantic import Field from petstore_api.models.client import Client diff --git a/samples/openapi3/client/petstore/python/petstore_api/api/default_api.py b/samples/openapi3/client/petstore/python/petstore_api/api/default_api.py index 3b615de2250..b5269287737 100755 --- a/samples/openapi3/client/petstore/python/petstore_api/api/default_api.py +++ b/samples/openapi3/client/petstore/python/petstore_api/api/default_api.py @@ -17,7 +17,6 @@ import io import warnings from pydantic import validate_arguments, ValidationError -from typing_extensions import Annotated from petstore_api.models.foo_get_default_response import FooGetDefaultResponse diff --git a/samples/openapi3/client/petstore/python/petstore_api/api/fake_api.py b/samples/openapi3/client/petstore/python/petstore_api/api/fake_api.py index df112200f14..1bdbf4e06a9 100755 --- a/samples/openapi3/client/petstore/python/petstore_api/api/fake_api.py +++ b/samples/openapi3/client/petstore/python/petstore_api/api/fake_api.py @@ -17,8 +17,8 @@ import io import warnings from pydantic import validate_arguments, ValidationError -from typing_extensions import Annotated +from typing_extensions import Annotated from datetime import date, datetime from pydantic import Field, StrictBool, StrictBytes, StrictFloat, StrictInt, StrictStr, conbytes, confloat, conint, conlist, constr, validator diff --git a/samples/openapi3/client/petstore/python/petstore_api/api/fake_classname_tags123_api.py b/samples/openapi3/client/petstore/python/petstore_api/api/fake_classname_tags123_api.py index 56620e1baa2..acfe8c01a94 100644 --- a/samples/openapi3/client/petstore/python/petstore_api/api/fake_classname_tags123_api.py +++ b/samples/openapi3/client/petstore/python/petstore_api/api/fake_classname_tags123_api.py @@ -17,8 +17,8 @@ import io import warnings from pydantic import validate_arguments, ValidationError -from typing_extensions import Annotated +from typing_extensions import Annotated from pydantic import Field from petstore_api.models.client import Client diff --git a/samples/openapi3/client/petstore/python/petstore_api/api/pet_api.py b/samples/openapi3/client/petstore/python/petstore_api/api/pet_api.py index 0325f335357..f0eefd7ec89 100755 --- a/samples/openapi3/client/petstore/python/petstore_api/api/pet_api.py +++ b/samples/openapi3/client/petstore/python/petstore_api/api/pet_api.py @@ -17,8 +17,8 @@ import io import warnings from pydantic import validate_arguments, ValidationError -from typing_extensions import Annotated +from typing_extensions import Annotated from pydantic import Field, StrictBytes, StrictInt, StrictStr, conlist, validator from typing import List, Optional, Union diff --git a/samples/openapi3/client/petstore/python/petstore_api/api/store_api.py b/samples/openapi3/client/petstore/python/petstore_api/api/store_api.py index 9ccbc98603d..b5b845531fe 100755 --- a/samples/openapi3/client/petstore/python/petstore_api/api/store_api.py +++ b/samples/openapi3/client/petstore/python/petstore_api/api/store_api.py @@ -17,8 +17,8 @@ import io import warnings from pydantic import validate_arguments, ValidationError -from typing_extensions import Annotated +from typing_extensions import Annotated from pydantic import Field, StrictStr, conint from typing import Dict diff --git a/samples/openapi3/client/petstore/python/petstore_api/api/user_api.py b/samples/openapi3/client/petstore/python/petstore_api/api/user_api.py index 3bb8e99417d..240a692ebca 100755 --- a/samples/openapi3/client/petstore/python/petstore_api/api/user_api.py +++ b/samples/openapi3/client/petstore/python/petstore_api/api/user_api.py @@ -17,8 +17,8 @@ import io import warnings from pydantic import validate_arguments, ValidationError -from typing_extensions import Annotated +from typing_extensions import Annotated from pydantic import Field, StrictStr, conlist from petstore_api.models.user import User