Skip to content

Commit 3da206c

Browse files
authored
🎨 Update internal types for Python 3.10 (fastapi#14898)
1 parent cc903bd commit 3da206c

20 files changed

Lines changed: 1087 additions & 1110 deletions

fastapi/_compat/v2.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@
88
from typing import (
99
Annotated,
1010
Any,
11+
Literal,
1112
Union,
1213
cast,
14+
get_args,
15+
get_origin,
1316
)
1417

1518
from fastapi._compat import lenient_issubclass, shared
@@ -32,7 +35,6 @@
3235
from pydantic_core.core_schema import (
3336
with_info_plain_validator_function as with_info_plain_validator_function,
3437
)
35-
from typing_extensions import Literal, get_args, get_origin
3638

3739
RequiredParam = PydanticUndefined
3840
Undefined = PydanticUndefined
@@ -83,22 +85,22 @@ class ModelField:
8385
field_info: FieldInfo
8486
name: str
8587
mode: Literal["validation", "serialization"] = "validation"
86-
config: Union[ConfigDict, None] = None
88+
config: ConfigDict | None = None
8789

8890
@property
8991
def alias(self) -> str:
9092
a = self.field_info.alias
9193
return a if a is not None else self.name
9294

9395
@property
94-
def validation_alias(self) -> Union[str, None]:
96+
def validation_alias(self) -> str | None:
9597
va = self.field_info.validation_alias
9698
if isinstance(va, str) and va:
9799
return va
98100
return None
99101

100102
@property
101-
def serialization_alias(self) -> Union[str, None]:
103+
def serialization_alias(self) -> str | None:
102104
sa = self.field_info.serialization_alias
103105
return sa or None
104106

@@ -143,7 +145,7 @@ def validate(
143145
value: Any,
144146
values: dict[str, Any] = {}, # noqa: B006
145147
*,
146-
loc: tuple[Union[int, str], ...] = (),
148+
loc: tuple[int | str, ...] = (),
147149
) -> tuple[Any, list[dict[str, Any]]]:
148150
try:
149151
return (
@@ -160,8 +162,8 @@ def serialize(
160162
value: Any,
161163
*,
162164
mode: Literal["json", "python"] = "json",
163-
include: Union[IncEx, None] = None,
164-
exclude: Union[IncEx, None] = None,
165+
include: IncEx | None = None,
166+
exclude: IncEx | None = None,
165167
by_alias: bool = True,
166168
exclude_unset: bool = False,
167169
exclude_defaults: bool = False,
@@ -202,7 +204,7 @@ def get_schema_from_model_field(
202204
],
203205
separate_input_output_schemas: bool = True,
204206
) -> dict[str, Any]:
205-
override_mode: Union[Literal["validation"], None] = (
207+
override_mode: Literal["validation"] | None = (
206208
None
207209
if (separate_input_output_schemas or _has_computed_fields(field))
208210
else "validation"
@@ -318,7 +320,7 @@ def serialize_sequence_value(*, field: ModelField, value: Any) -> Sequence[Any]:
318320
return shared.sequence_annotation_to_type[origin_type](value) # type: ignore[no-any-return,index]
319321

320322

321-
def get_missing_field_error(loc: tuple[Union[int, str], ...]) -> dict[str, Any]:
323+
def get_missing_field_error(loc: tuple[int | str, ...]) -> dict[str, Any]:
322324
error = ValidationError.from_exception_data(
323325
"Field required", [{"type": "missing", "loc": loc, "input": {}}]
324326
).errors(include_url=False)[0]
@@ -360,7 +362,7 @@ def get_cached_model_fields(model: type[BaseModel]) -> list[ModelField]:
360362
# Duplicate of several schema functions from Pydantic v1 to make them compatible with
361363
# Pydantic v2 and allow mixing the models
362364

363-
TypeModelOrEnum = Union[type["BaseModel"], type[Enum]]
365+
TypeModelOrEnum = type["BaseModel"] | type[Enum]
364366
TypeModelSet = set[TypeModelOrEnum]
365367

366368

@@ -377,7 +379,7 @@ def get_model_name_map(unique_models: TypeModelSet) -> dict[TypeModelOrEnum, str
377379

378380

379381
def get_flat_models_from_model(
380-
model: type["BaseModel"], known_models: Union[TypeModelSet, None] = None
382+
model: type["BaseModel"], known_models: TypeModelSet | None = None
381383
) -> TypeModelSet:
382384
known_models = known_models or set()
383385
fields = get_model_fields(model)
@@ -426,7 +428,7 @@ def get_flat_models_from_fields(
426428

427429

428430
def _regenerate_error_with_loc(
429-
*, errors: Sequence[Any], loc_prefix: tuple[Union[str, int], ...]
431+
*, errors: Sequence[Any], loc_prefix: tuple[str | int, ...]
430432
) -> list[dict[str, Any]]:
431433
updated_loc_errors: list[Any] = [
432434
{**err, "loc": loc_prefix + err.get("loc", ())} for err in errors

0 commit comments

Comments
 (0)