Skip to content

Commit f367649

Browse files
committed
chore: Type checking and formatting
1 parent ab4c085 commit f367649

File tree

4 files changed

+26
-25
lines changed

4 files changed

+26
-25
lines changed

openapi_python_client/schema/openapi_schema_pydantic/reference.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
from typing import Annotated, Any, Literal, TypeVar
1+
from typing import Annotated, Any, Literal, TypeVar, Union
22

33
from pydantic import BaseModel, ConfigDict, Discriminator, Field, Tag
44
from pydantic_core import ArgsKwargs
5+
from typing_extensions import TypeAlias
56

67

78
class Reference(BaseModel):
@@ -29,18 +30,17 @@ class Reference(BaseModel):
2930
)
3031

3132

32-
T = TypeVar('T')
33+
T = TypeVar("T")
3334

3435

35-
def _reference_discriminator(obj: Any) -> Literal['ref', 'other']:
36+
def _reference_discriminator(obj: Any) -> Literal["ref", "other"]:
3637
if isinstance(obj, dict):
37-
return 'ref' if '$ref' in obj else 'other'
38+
return "ref" if "$ref" in obj else "other"
3839
if isinstance(obj, ArgsKwargs):
39-
return 'ref' if 'ref' in obj.kwargs else 'other'
40-
return 'ref' if isinstance(obj, Reference) else 'other'
40+
return "ref" if obj.kwargs and "ref" in obj.kwargs else "other"
41+
return "ref" if isinstance(obj, Reference) else "other"
4142

4243

43-
ReferenceOr = Annotated[
44-
Annotated[Reference, Tag('ref')] |
45-
Annotated[T, Tag('other')],
46-
Discriminator(_reference_discriminator)]
44+
ReferenceOr: TypeAlias = Annotated[
45+
Union[Annotated[Reference, Tag("ref")], Annotated[T, Tag("other")]], Discriminator(_reference_discriminator)
46+
]

openapi_python_client/schema/openapi_schema_pydantic/responses.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
from .reference import ReferenceOr
32
from .response import Response
43

openapi_python_client/schema/openapi_schema_pydantic/schema.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class Schema(BaseModel):
4343
anyOf: list[ReferenceOr["Schema"]] = Field(default_factory=list)
4444
schema_not: Optional[ReferenceOr["Schema"]] = Field(default=None, alias="not")
4545
items: Optional[ReferenceOr["Schema"]] = None
46-
prefixItems: Optional[list[ReferenceOr["Schema"]]] = Field(default_factory=list)
46+
prefixItems: list[ReferenceOr["Schema"]] = Field(default_factory=list)
4747
properties: Optional[dict[str, ReferenceOr["Schema"]]] = None
4848
additionalProperties: Optional[Union[bool, ReferenceOr["Schema"]]] = None
4949
description: Optional[str] = None

tests/test_schema/test_noisy_refs.py

+15-13
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
# References:
1212
# [1] https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#reference-object
1313
# [2] https://docs.pydantic.dev/latest/concepts/unions/#smart-mode
14-
import json
1514
from typing import Annotated, TypeVar, Union, get_args, get_origin
1615

1716
import pytest
@@ -56,18 +55,21 @@ def deannotate_type(t):
5655
# The following types occur in various models, so we want to make sure they
5756
# parse properly. They are verified to /fail/ to parse as of commit 3bd12f86.
5857

59-
@pytest.mark.parametrize(("ref_or_type", "get_example_fn"), [
60-
(ReferenceOr[Callback], lambda t: {"test1": get_example(PathItem),
61-
"test2": get_example(PathItem)}),
62-
(ReferenceOr[Example], get_example),
63-
(ReferenceOr[Header], get_example),
64-
(ReferenceOr[Link], get_example),
65-
(ReferenceOr[Parameter], get_example),
66-
(ReferenceOr[RequestBody], get_example),
67-
(ReferenceOr[Response], get_example),
68-
(ReferenceOr[Schema], get_example),
69-
(ReferenceOr[SecurityScheme], get_example),
70-
])
58+
59+
@pytest.mark.parametrize(
60+
("ref_or_type", "get_example_fn"),
61+
[
62+
(ReferenceOr[Callback], lambda t: {"test1": get_example(PathItem), "test2": get_example(PathItem)}),
63+
(ReferenceOr[Example], get_example),
64+
(ReferenceOr[Header], get_example),
65+
(ReferenceOr[Link], get_example),
66+
(ReferenceOr[Parameter], get_example),
67+
(ReferenceOr[RequestBody], get_example),
68+
(ReferenceOr[Response], get_example),
69+
(ReferenceOr[Schema], get_example),
70+
(ReferenceOr[SecurityScheme], get_example),
71+
],
72+
)
7173
def test_type(ref_or_type, get_example_fn):
7274
base_type = None
7375
for maybe_annotated_type in get_args(deannotate_type(ref_or_type)):

0 commit comments

Comments
 (0)