From d69d5fe42f28332c1a6acff28f5f02aae9a46c8c Mon Sep 17 00:00:00 2001 From: Yugo Kato Date: Wed, 2 Oct 2024 12:34:38 -0700 Subject: [PATCH] Change to use the alias version of typing.Optional (temporary workaround for https://github.com/astral-sh/ruff/issues/4858) --- pyproject.toml | 2 -- .../clients/demo_app/api/base/demo_app_api.py | 8 ++++---- src/openapi_test_client/clients/demo_app/api/users.py | 4 ++-- .../clients/demo_app/models/users.py | 4 ++-- .../libraries/api/api_functions/utils/param_model.py | 5 ++++- src/openapi_test_client/libraries/api/types.py | 11 ++++++++++- 6 files changed, 22 insertions(+), 12 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 2a89ac2..54a4286 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -69,5 +69,3 @@ ignore = ["E731", "E741", "F403"] [tool.ruff.lint.per-file-ignores] "__init__.py" = ["F401"] -# We currently use `Optional` in a special way -"**/{clients,}/**/{api,models}/*" = ["UP007"] diff --git a/src/openapi_test_client/clients/demo_app/api/base/demo_app_api.py b/src/openapi_test_client/clients/demo_app/api/base/demo_app_api.py index 84f80ad..aacf8a5 100644 --- a/src/openapi_test_client/clients/demo_app/api/base/demo_app_api.py +++ b/src/openapi_test_client/clients/demo_app/api/base/demo_app_api.py @@ -1,6 +1,6 @@ from __future__ import annotations -from typing import TYPE_CHECKING, Optional +from typing import TYPE_CHECKING from common_libs.clients.rest_client import RestResponse from requests.exceptions import RequestException @@ -17,13 +17,13 @@ class DemoAppBaseAPI(APIBase): """Base class for demo_app API classes""" app_name = "demo_app" - endpoints: Optional[list[Endpoint]] = None + endpoints: list[Endpoint] | None = None def post_request_hook( self, endpoint: Endpoint, - response: Optional[RestResponse], - request_exception: Optional[RequestException], + response: RestResponse | None, + request_exception: RequestException | None, *path_params, **params, ): diff --git a/src/openapi_test_client/clients/demo_app/api/users.py b/src/openapi_test_client/clients/demo_app/api/users.py index 2ecc1d1..41e7fd5 100644 --- a/src/openapi_test_client/clients/demo_app/api/users.py +++ b/src/openapi_test_client/clients/demo_app/api/users.py @@ -1,10 +1,10 @@ -from typing import Annotated, Literal, Optional +from typing import Annotated, Literal from common_libs.clients.rest_client import RestResponse from openapi_test_client.clients.demo_app.api.base import DemoAppBaseAPI from openapi_test_client.libraries.api.api_functions import endpoint -from openapi_test_client.libraries.api.types import Constraint, File, Format +from openapi_test_client.libraries.api.types import Constraint, File, Format, Optional from ..models.users import Metadata diff --git a/src/openapi_test_client/clients/demo_app/models/users.py b/src/openapi_test_client/clients/demo_app/models/users.py index 0a4f35d..13a018c 100644 --- a/src/openapi_test_client/clients/demo_app/models/users.py +++ b/src/openapi_test_client/clients/demo_app/models/users.py @@ -4,9 +4,9 @@ """ from dataclasses import dataclass -from typing import Annotated, Literal, Optional +from typing import Annotated, Literal -from openapi_test_client.libraries.api.types import Constraint, Format, ParamModel +from openapi_test_client.libraries.api.types import Constraint, Format, Optional, ParamModel @dataclass diff --git a/src/openapi_test_client/libraries/api/api_functions/utils/param_model.py b/src/openapi_test_client/libraries/api/api_functions/utils/param_model.py index c61d53c..ffc16ae 100644 --- a/src/openapi_test_client/libraries/api/api_functions/utils/param_model.py +++ b/src/openapi_test_client/libraries/api/api_functions/utils/param_model.py @@ -13,6 +13,7 @@ from common_libs.utils import clean_obj_name import openapi_test_client.libraries.api.api_functions.utils.param_type as param_type_util +import openapi_test_client.libraries.api.types as types_module from openapi_test_client.libraries.api.types import ( Alias, DataclassModel, @@ -152,7 +153,9 @@ def generate_imports_code(obj_type: Any): [generate_imports_code(m) for m in [x for x in get_args(obj_type)]] elif typing_origin in [UnionType, Union]: if param_type_util.is_optional_type(obj_type): - module_and_name_pairs.append(("typing", Optional.__name__)) + # NOTE: We will use our alias version of typing.Optional for now + # module_and_name_pairs.append(("typing", Optional.__name__)) + module_and_name_pairs.append((types_module.__name__, Optional.__name__)) [generate_imports_code(m) for m in get_args(obj_type)] else: raise NotImplementedError(f"Unsupported typing origin: {typing_origin}") diff --git a/src/openapi_test_client/libraries/api/types.py b/src/openapi_test_client/libraries/api/types.py index 746f898..85b322f 100644 --- a/src/openapi_test_client/libraries/api/types.py +++ b/src/openapi_test_client/libraries/api/types.py @@ -5,7 +5,7 @@ from dataclasses import _DataclassParams # noqa from dataclasses import MISSING, Field, asdict, astuple, dataclass, field, is_dataclass, make_dataclass from functools import lru_cache -from typing import TYPE_CHECKING, Any, ClassVar, cast +from typing import TYPE_CHECKING, Any, ClassVar, TypeVar, cast from common_libs.decorators import freeze_args from common_libs.hash import HashableDict @@ -21,6 +21,15 @@ Protocol = object +T = TypeVar("T") + +# As a workaround for https://github.com/astral-sh/ruff/issues/4858, we temporarily define an alias of typing.Optional +# to avoid UP007 been reported by ruff for API classes and models, where we currently intentionally use `Optional` to +# indicate the endpoint/model parameter is optional. We may switch to use our own custom type for this purpose +# in the future since typing.Optional doesn't actually mean optional, but it just means nullable +Optional = T | None + + class ParamDef(HashableDict): """A class to store OpenAPI parameter object data"""