diff --git a/modules/openapi-generator/src/main/resources/python/README_onlypackage.mustache b/modules/openapi-generator/src/main/resources/python/README_onlypackage.mustache index d205966d0bc5..b57f9ea985a1 100644 --- a/modules/openapi-generator/src/main/resources/python/README_onlypackage.mustache +++ b/modules/openapi-generator/src/main/resources/python/README_onlypackage.mustache @@ -30,7 +30,7 @@ To be able to use it, you will need these dependencies in your own package that * python-dateutil >= 2.8.2 {{#async}} * aiohttp >= 3.8.4 -* aiohttp-retry >= 2.8.3 +* aiohttp-retry >= 2.8.3 (only when retries are configured) {{/async}} {{#hasHttpSignatureMethods}} * pem >= 19.3.0 diff --git a/modules/openapi-generator/src/main/resources/python/asyncio/rest.mustache b/modules/openapi-generator/src/main/resources/python/asyncio/rest.mustache index 220c8335c36f..38146b8d9c1e 100644 --- a/modules/openapi-generator/src/main/resources/python/asyncio/rest.mustache +++ b/modules/openapi-generator/src/main/resources/python/asyncio/rest.mustache @@ -7,14 +7,16 @@ import io import json import re import ssl -from typing import Any, Dict, Optional, Union +from typing import TYPE_CHECKING, Any, Dict, Optional, Union import aiohttp -import aiohttp_retry from multidict import CIMultiDict from {{packageName}}.exceptions import ApiException, ApiValueError +if TYPE_CHECKING: + import aiohttp_retry + RESTResponseType = aiohttp.ClientResponse ALLOW_RETRY_METHODS = frozenset({'DELETE', 'GET', 'HEAD', 'OPTIONS', 'PUT', 'TRACE'}) @@ -76,20 +78,23 @@ class RESTClientObject: retries = configuration.retries if retries is None: self._effective_retry_options = None - elif isinstance(retries, aiohttp_retry.RetryOptionsBase): - self._effective_retry_options = retries - elif isinstance(retries, int): - self._effective_retry_options = aiohttp_retry.ExponentialRetry( - attempts=retries, - factor=2.0, - start_timeout=0.1, - max_timeout=120.0 - ) else: - self._effective_retry_options = None + import aiohttp_retry + + if isinstance(retries, aiohttp_retry.RetryOptionsBase): + self._effective_retry_options = retries + elif isinstance(retries, int): + self._effective_retry_options = aiohttp_retry.ExponentialRetry( + attempts=retries, + factor=2.0, + start_timeout=0.1, + max_timeout=120.0 + ) + else: + self._effective_retry_options = None self.pool_manager: Optional[aiohttp.ClientSession] = None - self.retry_client: Optional[aiohttp_retry.RetryClient] = None + self.retry_client: Optional["aiohttp_retry.RetryClient"] = None async def close(self) -> None: if self.pool_manager: @@ -256,7 +261,7 @@ class RESTClientObject: declared content type.""" raise ApiException(status=0, reason=msg) - pool_manager: Union[aiohttp.ClientSession, aiohttp_retry.RetryClient] + pool_manager: Union[aiohttp.ClientSession, "aiohttp_retry.RetryClient"] # https pool manager if self.pool_manager is None: @@ -265,6 +270,8 @@ class RESTClientObject: if self._effective_retry_options is not None and method in ALLOW_RETRY_METHODS: if self.retry_client is None: + import aiohttp_retry + self.retry_client = aiohttp_retry.RetryClient( client_session=self.pool_manager, retry_options=self._effective_retry_options diff --git a/modules/openapi-generator/src/main/resources/python/configuration.mustache b/modules/openapi-generator/src/main/resources/python/configuration.mustache index 920408100449..267be2f4da34 100644 --- a/modules/openapi-generator/src/main/resources/python/configuration.mustache +++ b/modules/openapi-generator/src/main/resources/python/configuration.mustache @@ -2,7 +2,6 @@ {{#asyncio}} import aiohttp -import aiohttp_retry {{/asyncio}} {{#async}} import base64 @@ -24,6 +23,13 @@ from urllib.parse import urlparse from urllib.request import getproxies {{/async}} from typing_extensions import NotRequired, Self +{{#asyncio}} + +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + import aiohttp_retry +{{/asyncio}} {{^async}} import urllib3 @@ -344,7 +350,7 @@ conf = {{{packageName}}}.Configuration( ignore_operation_servers: bool=False, ssl_ca_cert: Optional[str]=None, {{#asyncio}} - retries: Optional[Union[int, aiohttp_retry.RetryOptionsBase]] = None, + retries: Optional[Union[int, "aiohttp_retry.RetryOptionsBase"]] = None, trace_configs: Optional[List[aiohttp.TraceConfig]] = None, tcp_connector_limit_per_host: Optional[int] = None, client_session_kwargs: Optional[Dict[str, Any]] = None, diff --git a/modules/openapi-generator/src/main/resources/python/pyproject.mustache b/modules/openapi-generator/src/main/resources/python/pyproject.mustache index 109cff467caa..c459d2315a35 100644 --- a/modules/openapi-generator/src/main/resources/python/pyproject.mustache +++ b/modules/openapi-generator/src/main/resources/python/pyproject.mustache @@ -44,7 +44,7 @@ urllib3 = ">= 2.7.0, < 3.0.0" python-dateutil = ">= 2.8.2" {{#asyncio}} aiohttp = ">= 3.14.1" -aiohttp-retry = ">= 2.8.3" +aiohttp-retry = { version = ">= 2.8.3", optional = true } {{/asyncio}} {{#httpx}} httpx = ">= 0.28.1" @@ -58,6 +58,11 @@ pycryptodome = ">= 3.9.0" {{/hasHttpSignatureMethods}} pydantic = ">= 2.11" typing-extensions = ">= 4.7.1" +{{#asyncio}} + +[tool.poetry.extras] +retry = ["aiohttp-retry"] +{{/asyncio}} {{/poetry1}} {{^poetry1}} requires-python = ">=3.11" @@ -75,7 +80,6 @@ dependencies = [ {{/httpx2}} {{#asyncio}} "aiohttp (>=3.14.1)", - "aiohttp-retry (>=2.8.3)", {{/asyncio}} {{#hasHttpSignatureMethods}} "pem (>=19.3.0)", @@ -84,6 +88,11 @@ dependencies = [ "pydantic (>=2.11)", "typing-extensions (>=4.7.1)", ] +{{#asyncio}} + +[project.optional-dependencies] +retry = ["aiohttp-retry (>=2.8.3)"] +{{/asyncio}} [project.urls] Repository = "https://{{{gitHost}}}/{{{gitUserId}}}/{{{gitRepoId}}}" @@ -98,6 +107,9 @@ tox = ">= 3.9.0" flake8 = ">= 4.0.0" types-python-dateutil = ">= 2.8.19.14" mypy = ">= 1.5" +{{#asyncio}} +aiohttp-retry = ">= 2.8.3" +{{/asyncio}} {{/poetry1}} {{^poetry1}} [dependency-groups] @@ -108,6 +120,9 @@ dev = [ "flake8>=4.0.0", "types-python-dateutil>=2.8.19.14", "mypy>=1.5", +{{#asyncio}} + "aiohttp-retry>=2.8.3", +{{/asyncio}} ] {{/poetry1}} diff --git a/modules/openapi-generator/src/main/resources/python/requirements.mustache b/modules/openapi-generator/src/main/resources/python/requirements.mustache index 36f07decbdbf..afcc97cf5dbf 100644 --- a/modules/openapi-generator/src/main/resources/python/requirements.mustache +++ b/modules/openapi-generator/src/main/resources/python/requirements.mustache @@ -4,7 +4,6 @@ urllib3 >= 2.7.0, < 3.0.0 python_dateutil >= 2.8.2 {{#asyncio}} aiohttp >= 3.14.1 -aiohttp-retry >= 2.8.3 {{/asyncio}} {{#httpx}} httpx >= 0.28.1 diff --git a/modules/openapi-generator/src/main/resources/python/setup.mustache b/modules/openapi-generator/src/main/resources/python/setup.mustache index 8f0da3eee18d..813ffa6db69a 100644 --- a/modules/openapi-generator/src/main/resources/python/setup.mustache +++ b/modules/openapi-generator/src/main/resources/python/setup.mustache @@ -18,7 +18,6 @@ REQUIRES = [ "python-dateutil >= 2.8.2", {{#asyncio}} "aiohttp >= 3.14.1", - "aiohttp-retry >= 2.8.3", {{/asyncio}} {{#httpx}} "httpx >= 0.28.1", @@ -43,6 +42,9 @@ setup( url="{{packageUrl}}", keywords=["OpenAPI", "OpenAPI-Generator", "{{{appName}}}"], install_requires=REQUIRES, +{{#asyncio}} + extras_require={"retry": ["aiohttp-retry >= 2.8.3"]}, +{{/asyncio}} {{#sourceFolder}} package_dir={"": "{{{sourceFolder}}}"}, {{/sourceFolder}} diff --git a/modules/openapi-generator/src/main/resources/python/test-requirements.mustache b/modules/openapi-generator/src/main/resources/python/test-requirements.mustache index 9cb0629aded0..cd330a5118e5 100644 --- a/modules/openapi-generator/src/main/resources/python/test-requirements.mustache +++ b/modules/openapi-generator/src/main/resources/python/test-requirements.mustache @@ -4,3 +4,6 @@ tox >= 3.9.0 flake8 >= 4.0.0 types-python-dateutil >= 2.8.19.14 mypy >= 1.5 +{{#asyncio}} +aiohttp-retry >= 2.8.3 +{{/asyncio}} diff --git a/samples/openapi3/client/petstore/python-aiohttp/petstore_api/configuration.py b/samples/openapi3/client/petstore/python-aiohttp/petstore_api/configuration.py index 38f5a69aeef3..673e6daf5d4d 100644 --- a/samples/openapi3/client/petstore/python-aiohttp/petstore_api/configuration.py +++ b/samples/openapi3/client/petstore/python-aiohttp/petstore_api/configuration.py @@ -11,7 +11,6 @@ import aiohttp -import aiohttp_retry import base64 import copy import http.client as httplib @@ -21,6 +20,11 @@ from typing import Any, ClassVar, Dict, List, Literal, Optional, TypedDict, Union from typing_extensions import NotRequired, Self +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + import aiohttp_retry + from petstore_api.signing import HttpSigningConfiguration @@ -288,7 +292,7 @@ def __init__( server_operation_variables: Optional[Dict[int, ServerVariablesT]]=None, ignore_operation_servers: bool=False, ssl_ca_cert: Optional[str]=None, - retries: Optional[Union[int, aiohttp_retry.RetryOptionsBase]] = None, + retries: Optional[Union[int, "aiohttp_retry.RetryOptionsBase"]] = None, trace_configs: Optional[List[aiohttp.TraceConfig]] = None, tcp_connector_limit_per_host: Optional[int] = None, client_session_kwargs: Optional[Dict[str, Any]] = None, diff --git a/samples/openapi3/client/petstore/python-aiohttp/petstore_api/rest.py b/samples/openapi3/client/petstore/python-aiohttp/petstore_api/rest.py index f9c8d3b2bab2..3db17d4f4b1b 100644 --- a/samples/openapi3/client/petstore/python-aiohttp/petstore_api/rest.py +++ b/samples/openapi3/client/petstore/python-aiohttp/petstore_api/rest.py @@ -16,14 +16,16 @@ import json import re import ssl -from typing import Any, Dict, Optional, Union +from typing import TYPE_CHECKING, Any, Dict, Optional, Union import aiohttp -import aiohttp_retry from multidict import CIMultiDict from petstore_api.exceptions import ApiException, ApiValueError +if TYPE_CHECKING: + import aiohttp_retry + RESTResponseType = aiohttp.ClientResponse ALLOW_RETRY_METHODS = frozenset({'DELETE', 'GET', 'HEAD', 'OPTIONS', 'PUT', 'TRACE'}) @@ -85,20 +87,23 @@ def __init__(self, configuration) -> None: retries = configuration.retries if retries is None: self._effective_retry_options = None - elif isinstance(retries, aiohttp_retry.RetryOptionsBase): - self._effective_retry_options = retries - elif isinstance(retries, int): - self._effective_retry_options = aiohttp_retry.ExponentialRetry( - attempts=retries, - factor=2.0, - start_timeout=0.1, - max_timeout=120.0 - ) else: - self._effective_retry_options = None + import aiohttp_retry + + if isinstance(retries, aiohttp_retry.RetryOptionsBase): + self._effective_retry_options = retries + elif isinstance(retries, int): + self._effective_retry_options = aiohttp_retry.ExponentialRetry( + attempts=retries, + factor=2.0, + start_timeout=0.1, + max_timeout=120.0 + ) + else: + self._effective_retry_options = None self.pool_manager: Optional[aiohttp.ClientSession] = None - self.retry_client: Optional[aiohttp_retry.RetryClient] = None + self.retry_client: Optional["aiohttp_retry.RetryClient"] = None async def close(self) -> None: if self.pool_manager: @@ -258,7 +263,7 @@ async def request( declared content type.""" raise ApiException(status=0, reason=msg) - pool_manager: Union[aiohttp.ClientSession, aiohttp_retry.RetryClient] + pool_manager: Union[aiohttp.ClientSession, "aiohttp_retry.RetryClient"] # https pool manager if self.pool_manager is None: @@ -267,6 +272,8 @@ async def request( if self._effective_retry_options is not None and method in ALLOW_RETRY_METHODS: if self.retry_client is None: + import aiohttp_retry + self.retry_client = aiohttp_retry.RetryClient( client_session=self.pool_manager, retry_options=self._effective_retry_options diff --git a/samples/openapi3/client/petstore/python-aiohttp/pyproject.toml b/samples/openapi3/client/petstore/python-aiohttp/pyproject.toml index bcf671a77754..f18a5493567f 100644 --- a/samples/openapi3/client/petstore/python-aiohttp/pyproject.toml +++ b/samples/openapi3/client/petstore/python-aiohttp/pyproject.toml @@ -13,11 +13,14 @@ include = ["petstore_api/py.typed"] python = "^3.11" python-dateutil = ">= 2.8.2" aiohttp = ">= 3.14.1" -aiohttp-retry = ">= 2.8.3" +aiohttp-retry = { version = ">= 2.8.3", optional = true } pem = ">= 19.3.0" pycryptodome = ">= 3.9.0" pydantic = ">= 2.11" typing-extensions = ">= 4.7.1" + +[tool.poetry.extras] +retry = ["aiohttp-retry"] # Deprecated by Poetry; retained for compatibility with legacy Poetry 1 releases. [tool.poetry.dev-dependencies] pytest = ">= 9.0.3" @@ -26,6 +29,7 @@ tox = ">= 3.9.0" flake8 = ">= 4.0.0" types-python-dateutil = ">= 2.8.19.14" mypy = ">= 1.5" +aiohttp-retry = ">= 2.8.3" [build-system] diff --git a/samples/openapi3/client/petstore/python-aiohttp/requirements.txt b/samples/openapi3/client/petstore/python-aiohttp/requirements.txt index baeace9e16c0..6c9977c4778f 100644 --- a/samples/openapi3/client/petstore/python-aiohttp/requirements.txt +++ b/samples/openapi3/client/petstore/python-aiohttp/requirements.txt @@ -1,6 +1,5 @@ python_dateutil >= 2.8.2 aiohttp >= 3.14.1 -aiohttp-retry >= 2.8.3 pem >= 19.3.0 pycryptodome >= 3.9.0 pydantic >= 2.11 diff --git a/samples/openapi3/client/petstore/python-aiohttp/setup.py b/samples/openapi3/client/petstore/python-aiohttp/setup.py index 662f156c19cf..0bbcb43b4d01 100644 --- a/samples/openapi3/client/petstore/python-aiohttp/setup.py +++ b/samples/openapi3/client/petstore/python-aiohttp/setup.py @@ -24,7 +24,6 @@ REQUIRES = [ "python-dateutil >= 2.8.2", "aiohttp >= 3.14.1", - "aiohttp-retry >= 2.8.3", "pem >= 19.3.0", "pycryptodome >= 3.9.0", "pydantic >= 2.11", @@ -40,6 +39,7 @@ url="", keywords=["OpenAPI", "OpenAPI-Generator", "OpenAPI Petstore"], install_requires=REQUIRES, + extras_require={"retry": ["aiohttp-retry >= 2.8.3"]}, packages=find_packages(exclude=["test", "tests"]), include_package_data=True, license="Apache-2.0", diff --git a/samples/openapi3/client/petstore/python-aiohttp/test-requirements.txt b/samples/openapi3/client/petstore/python-aiohttp/test-requirements.txt index 9cb0629aded0..9f09b8fd5716 100644 --- a/samples/openapi3/client/petstore/python-aiohttp/test-requirements.txt +++ b/samples/openapi3/client/petstore/python-aiohttp/test-requirements.txt @@ -4,3 +4,4 @@ tox >= 3.9.0 flake8 >= 4.0.0 types-python-dateutil >= 2.8.19.14 mypy >= 1.5 +aiohttp-retry >= 2.8.3