Skip to content

Commit

Permalink
Change to use the alias version of typing.Optional (temporary workaro…
Browse files Browse the repository at this point in the history
  • Loading branch information
yugokato committed Oct 2, 2024
1 parent 588349f commit d69d5fe
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 12 deletions.
2 changes: 0 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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,
):
Expand Down
4 changes: 2 additions & 2 deletions src/openapi_test_client/clients/demo_app/api/users.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down
4 changes: 2 additions & 2 deletions src/openapi_test_client/clients/demo_app/models/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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}")
Expand Down
11 changes: 10 additions & 1 deletion src/openapi_test_client/libraries/api/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"""

Expand Down

0 comments on commit d69d5fe

Please sign in to comment.