From d10fa5df1139afe00416f3d8cb4ce6ca6021fab4 Mon Sep 17 00:00:00 2001 From: Yurii Motov Date: Wed, 25 Feb 2026 17:27:52 +0100 Subject: [PATCH] Update to Python 3.10 syntax --- .../test_body/test_nullable_and_defaults.py | 56 +++++++++---------- .../test_cookie/test_nullable_and_defaults.py | 26 ++++----- .../test_file/test_nullable_and_defaults.py | 22 ++++---- .../test_form/test_nullable_and_defaults.py | 38 ++++++------- .../test_header/test_nullable_and_defaults.py | 38 ++++++------- .../test_query/test_nullable_and_defaults.py | 38 ++++++------- 6 files changed, 109 insertions(+), 109 deletions(-) diff --git a/tests/test_request_params/test_body/test_nullable_and_defaults.py b/tests/test_request_params/test_body/test_nullable_and_defaults.py index e78811b22..e3d658ce7 100644 --- a/tests/test_request_params/test_body/test_nullable_and_defaults.py +++ b/tests/test_request_params/test_body/test_nullable_and_defaults.py @@ -1,4 +1,4 @@ -from typing import Annotated, Any, Union +from typing import Annotated, Any from unittest.mock import Mock, patch import pytest @@ -22,10 +22,10 @@ def convert(v: Any) -> Any: @app.post("/nullable-required") async def read_nullable_required( - int_val: Annotated[Union[int, None], Body(), BeforeValidator(lambda v: convert(v))], - str_val: Annotated[Union[str, None], Body(), BeforeValidator(lambda v: convert(v))], + int_val: Annotated[int | None, Body(), BeforeValidator(lambda v: convert(v))], + str_val: Annotated[str | None, Body(), BeforeValidator(lambda v: convert(v))], list_val: Annotated[ - Union[list[int], None], + list[int] | None, Body(), BeforeValidator(lambda v: convert(v)), ], @@ -39,9 +39,9 @@ async def read_nullable_required( class ModelNullableRequired(BaseModel): - int_val: Union[int, None] - str_val: Union[str, None] - list_val: Union[list[int], None] + int_val: int | None + str_val: str | None + list_val: list[int] | None @field_validator("*", mode="before") def validate_all(cls, v): @@ -60,14 +60,14 @@ async def read_model_nullable_required(params: ModelNullableRequired): @app.post("/nullable-required-str") async def read_nullable_required_no_embed_str( - str_val: Annotated[Union[str, None], Body(), BeforeValidator(lambda v: convert(v))], + str_val: Annotated[str | None, Body(), BeforeValidator(lambda v: convert(v))], ): return {"val": str_val} @app.post("/nullable-required-int") async def read_nullable_required_no_embed_int( - int_val: Annotated[Union[int, None], Body(), BeforeValidator(lambda v: convert(v))], + int_val: Annotated[int | None, Body(), BeforeValidator(lambda v: convert(v))], ): return {"val": int_val} @@ -75,7 +75,7 @@ async def read_nullable_required_no_embed_int( @app.post("/nullable-required-list") async def read_nullable_required_no_embed_list( list_val: Annotated[ - Union[list[int], None], Body(), BeforeValidator(lambda v: convert(v)) + list[int] | None, Body(), BeforeValidator(lambda v: convert(v)) ], ): return {"val": list_val} @@ -398,17 +398,17 @@ def test_nullable_required_no_embed_pass_value(path: str, value: Any): @app.post("/nullable-non-required") async def read_nullable_non_required( int_val: Annotated[ - Union[int, None], + int | None, Body(), BeforeValidator(lambda v: convert(v)), ] = None, str_val: Annotated[ - Union[str, None], + str | None, Body(), BeforeValidator(lambda v: convert(v)), ] = None, list_val: Annotated[ - Union[list[int], None], + list[int] | None, Body(), BeforeValidator(lambda v: convert(v)), ] = None, @@ -422,9 +422,9 @@ async def read_nullable_non_required( class ModelNullableNonRequired(BaseModel): - int_val: Union[int, None] = None - str_val: Union[str, None] = None - list_val: Union[list[int], None] = None + int_val: int | None = None + str_val: str | None = None + list_val: list[int] | None = None @field_validator("*", mode="before") def validate_all(cls, v): @@ -446,7 +446,7 @@ async def read_model_nullable_non_required( @app.post("/nullable-non-required-str") async def read_nullable_non_required_no_embed_str( str_val: Annotated[ - Union[str, None], + str | None, Body(), BeforeValidator(lambda v: convert(v)), ] = None, @@ -457,7 +457,7 @@ async def read_nullable_non_required_no_embed_str( @app.post("/nullable-non-required-int") async def read_nullable_non_required_no_embed_int( int_val: Annotated[ - Union[int, None], + int | None, Body(), BeforeValidator(lambda v: convert(v)), ] = None, @@ -468,7 +468,7 @@ async def read_nullable_non_required_no_embed_int( @app.post("/nullable-non-required-list") async def read_nullable_non_required_no_embed_list( list_val: Annotated[ - Union[list[int], None], + list[int] | None, Body(), BeforeValidator(lambda v: convert(v)), ] = None, @@ -746,17 +746,17 @@ def test_nullable_non_required_no_embed_pass_value(path: str, value: Any): async def read_nullable_with_non_null_default( *, int_val: Annotated[ - Union[int, None], + int | None, Body(), BeforeValidator(lambda v: convert(v)), ] = -1, str_val: Annotated[ - Union[str, None], + str | None, Body(), BeforeValidator(lambda v: convert(v)), ] = "default", list_val: Annotated[ - Union[list[int], None], + list[int] | None, Body(default_factory=lambda: [0]), BeforeValidator(lambda v: convert(v)), ], @@ -770,9 +770,9 @@ async def read_nullable_with_non_null_default( class ModelNullableWithNonNullDefault(BaseModel): - int_val: Union[int, None] = -1 - str_val: Union[str, None] = "default" - list_val: Union[list[int], None] = [0] + int_val: int | None = -1 + str_val: str | None = "default" + list_val: list[int] | None = [0] @field_validator("*", mode="before") def validate_all(cls, v): @@ -794,7 +794,7 @@ async def read_model_nullable_with_non_null_default( @app.post("/nullable-with-non-null-default-str") async def read_nullable_with_non_null_default_no_embed_str( str_val: Annotated[ - Union[str, None], + str | None, Body(), BeforeValidator(lambda v: convert(v)), ] = "default", @@ -805,7 +805,7 @@ async def read_nullable_with_non_null_default_no_embed_str( @app.post("/nullable-with-non-null-default-int") async def read_nullable_with_non_null_default_no_embed_int( int_val: Annotated[ - Union[int, None], + int | None, Body(), BeforeValidator(lambda v: convert(v)), ] = -1, @@ -816,7 +816,7 @@ async def read_nullable_with_non_null_default_no_embed_int( @app.post("/nullable-with-non-null-default-list") async def read_nullable_with_non_null_default_no_embed_list( list_val: Annotated[ - Union[list[int], None], + list[int] | None, Body(default_factory=lambda: [0]), BeforeValidator(lambda v: convert(v)), ], diff --git a/tests/test_request_params/test_cookie/test_nullable_and_defaults.py b/tests/test_request_params/test_cookie/test_nullable_and_defaults.py index 142df0e40..f757997dd 100644 --- a/tests/test_request_params/test_cookie/test_nullable_and_defaults.py +++ b/tests/test_request_params/test_cookie/test_nullable_and_defaults.py @@ -1,4 +1,4 @@ -from typing import Annotated, Any, Union +from typing import Annotated, Any from unittest.mock import Mock, patch import pytest @@ -21,12 +21,12 @@ def convert(v: Any) -> Any: @app.get("/nullable-required") async def read_nullable_required( int_val: Annotated[ - Union[int, None], + int | None, Cookie(), BeforeValidator(lambda v: convert(v)), ], str_val: Annotated[ - Union[str, None], + str | None, Cookie(), BeforeValidator(lambda v: convert(v)), ], @@ -39,8 +39,8 @@ async def read_nullable_required( class ModelNullableRequired(BaseModel): - int_val: Union[int, None] - str_val: Union[str, None] + int_val: int | None + str_val: str | None @field_validator("*", mode="before") @classmethod @@ -160,12 +160,12 @@ def test_nullable_required_pass_value(path: str, values: dict[str, str]): @app.get("/nullable-non-required") async def read_nullable_non_required( int_val: Annotated[ - Union[int, None], + int | None, Cookie(), BeforeValidator(lambda v: convert(v)), ] = None, str_val: Annotated[ - Union[str, None], + str | None, Cookie(), BeforeValidator(lambda v: convert(v)), ] = None, @@ -178,8 +178,8 @@ async def read_nullable_non_required( class ModelNullableNonRequired(BaseModel): - int_val: Union[int, None] = None - str_val: Union[str, None] = None + int_val: int | None = None + str_val: str | None = None @field_validator("*", mode="before") @classmethod @@ -293,12 +293,12 @@ def test_nullable_non_required_pass_value(path: str, values: dict[str, str]): async def read_nullable_with_non_null_default( *, int_val: Annotated[ - Union[int, None], + int | None, Cookie(), BeforeValidator(lambda v: convert(v)), ] = -1, str_val: Annotated[ - Union[str, None], + str | None, Cookie(), BeforeValidator(lambda v: convert(v)), ] = "default", @@ -311,8 +311,8 @@ async def read_nullable_with_non_null_default( class ModelNullableWithNonNullDefault(BaseModel): - int_val: Union[int, None] = -1 - str_val: Union[str, None] = "default" + int_val: int | None = -1 + str_val: str | None = "default" @field_validator("*", mode="before") @classmethod diff --git a/tests/test_request_params/test_file/test_nullable_and_defaults.py b/tests/test_request_params/test_file/test_nullable_and_defaults.py index 4c34889e5..80af58ded 100644 --- a/tests/test_request_params/test_file/test_nullable_and_defaults.py +++ b/tests/test_request_params/test_file/test_nullable_and_defaults.py @@ -1,4 +1,4 @@ -from typing import Annotated, Any, Union +from typing import Annotated, Any from unittest.mock import Mock, patch import pytest @@ -24,12 +24,12 @@ def convert(v: Any) -> Any: @app.post("/nullable-required-bytes") async def read_nullable_required_bytes( file: Annotated[ - Union[bytes, None], + bytes | None, File(), BeforeValidator(lambda v: convert(v)), ], files: Annotated[ - Union[list[bytes], None], + list[bytes] | None, File(), BeforeValidator(lambda v: convert(v)), ], @@ -43,12 +43,12 @@ async def read_nullable_required_bytes( @app.post("/nullable-required-uploadfile") async def read_nullable_required_uploadfile( file: Annotated[ - Union[UploadFile, None], + UploadFile | None, File(), BeforeValidator(lambda v: convert(v)), ], files: Annotated[ - Union[list[UploadFile], None], + list[UploadFile] | None, File(), BeforeValidator(lambda v: convert(v)), ], @@ -196,12 +196,12 @@ def test_nullable_required_pass_file(path: str): @app.post("/nullable-non-required-bytes") async def read_nullable_non_required_bytes( file: Annotated[ - Union[bytes, None], + bytes | None, File(), BeforeValidator(lambda v: convert(v)), ] = None, files: Annotated[ - Union[list[bytes], None], + list[bytes] | None, File(), BeforeValidator(lambda v: convert(v)), ] = None, @@ -215,12 +215,12 @@ async def read_nullable_non_required_bytes( @app.post("/nullable-non-required-uploadfile") async def read_nullable_non_required_uploadfile( file: Annotated[ - Union[UploadFile, None], + UploadFile | None, File(), BeforeValidator(lambda v: convert(v)), ] = None, files: Annotated[ - Union[list[UploadFile], None], + list[UploadFile] | None, File(), BeforeValidator(lambda v: convert(v)), ] = None, @@ -351,12 +351,12 @@ def test_nullable_non_required_pass_file(path: str): async def read_nullable_with_non_null_default_bytes( *, file: Annotated[ - Union[bytes, None], + bytes | None, File(), BeforeValidator(lambda v: convert(v)), ] = b"default", files: Annotated[ - Union[list[bytes], None], + list[bytes] | None, File(default_factory=lambda: [b"default"]), BeforeValidator(lambda v: convert(v)), ], diff --git a/tests/test_request_params/test_form/test_nullable_and_defaults.py b/tests/test_request_params/test_form/test_nullable_and_defaults.py index 558b28833..0e1d82c1f 100644 --- a/tests/test_request_params/test_form/test_nullable_and_defaults.py +++ b/tests/test_request_params/test_form/test_nullable_and_defaults.py @@ -1,4 +1,4 @@ -from typing import Annotated, Any, Union +from typing import Annotated, Any from unittest.mock import Mock, call, patch import pytest @@ -23,17 +23,17 @@ def convert(v: Any) -> Any: @app.post("/nullable-required") async def read_nullable_required( int_val: Annotated[ - Union[int, None], + int | None, Form(), BeforeValidator(lambda v: convert(v)), ], str_val: Annotated[ - Union[str, None], + str | None, Form(), BeforeValidator(lambda v: convert(v)), ], list_val: Annotated[ - Union[list[int], None], + list[int] | None, Form(), BeforeValidator(lambda v: convert(v)), ], @@ -47,9 +47,9 @@ async def read_nullable_required( class ModelNullableRequired(BaseModel): - int_val: Union[int, None] - str_val: Union[str, None] - list_val: Union[list[int], None] + int_val: int | None + str_val: str | None + list_val: list[int] | None @field_validator("*", mode="before") def convert_fields(cls, v): @@ -270,17 +270,17 @@ def test_nullable_required_pass_value(path: str): @app.post("/nullable-non-required") async def read_nullable_non_required( int_val: Annotated[ - Union[int, None], + int | None, Form(), BeforeValidator(lambda v: convert(v)), ] = None, str_val: Annotated[ - Union[str, None], + str | None, Form(), BeforeValidator(lambda v: convert(v)), ] = None, list_val: Annotated[ - Union[list[int], None], + list[int] | None, Form(), BeforeValidator(lambda v: convert(v)), ] = None, @@ -294,9 +294,9 @@ async def read_nullable_non_required( class ModelNullableNonRequired(BaseModel): - int_val: Union[int, None] = None - str_val: Union[str, None] = None - list_val: Union[list[int], None] = None + int_val: int | None = None + str_val: str | None = None + list_val: list[int] | None = None @field_validator("*", mode="before") def convert_fields(cls, v): @@ -492,17 +492,17 @@ def test_nullable_non_required_pass_value(path: str): async def read_nullable_with_non_null_default( *, int_val: Annotated[ - Union[int, None], + int | None, Form(), BeforeValidator(lambda v: convert(v)), ] = -1, str_val: Annotated[ - Union[str, None], + str | None, Form(), BeforeValidator(lambda v: convert(v)), ] = "default", list_val: Annotated[ - Union[list[int], None], + list[int] | None, Form(default_factory=lambda: [0]), BeforeValidator(lambda v: convert(v)), ], @@ -516,9 +516,9 @@ async def read_nullable_with_non_null_default( class ModelNullableWithNonNullDefault(BaseModel): - int_val: Union[int, None] = -1 - str_val: Union[str, None] = "default" - list_val: Union[list[int], None] = [0] + int_val: int | None = -1 + str_val: str | None = "default" + list_val: list[int] | None = [0] @field_validator("*", mode="before") def convert_fields(cls, v): diff --git a/tests/test_request_params/test_header/test_nullable_and_defaults.py b/tests/test_request_params/test_header/test_nullable_and_defaults.py index a7bfbb6c2..474fb0ef4 100644 --- a/tests/test_request_params/test_header/test_nullable_and_defaults.py +++ b/tests/test_request_params/test_header/test_nullable_and_defaults.py @@ -1,4 +1,4 @@ -from typing import Annotated, Any, Union +from typing import Annotated, Any from unittest.mock import Mock, patch import pytest @@ -21,17 +21,17 @@ def convert(v: Any) -> Any: @app.get("/nullable-required") async def read_nullable_required( int_val: Annotated[ - Union[int, None], + int | None, Header(), BeforeValidator(lambda v: convert(v)), ], str_val: Annotated[ - Union[str, None], + str | None, Header(), BeforeValidator(lambda v: convert(v)), ], list_val: Annotated[ - Union[list[int], None], + list[int] | None, Header(), BeforeValidator(lambda v: convert(v)), ], @@ -45,9 +45,9 @@ async def read_nullable_required( class ModelNullableRequired(BaseModel): - int_val: Union[int, None] - str_val: Union[str, None] - list_val: Union[list[int], None] + int_val: int | None + str_val: str | None + list_val: list[int] | None @field_validator("*", mode="before") @classmethod @@ -234,17 +234,17 @@ def test_nullable_required_pass_empty_str_to_str_val(path: str): @app.get("/nullable-non-required") async def read_nullable_non_required( int_val: Annotated[ - Union[int, None], + int | None, Header(), BeforeValidator(lambda v: convert(v)), ] = None, str_val: Annotated[ - Union[str, None], + str | None, Header(), BeforeValidator(lambda v: convert(v)), ] = None, list_val: Annotated[ - Union[list[int], None], + list[int] | None, Header(), BeforeValidator(lambda v: convert(v)), ] = None, @@ -258,9 +258,9 @@ async def read_nullable_non_required( class ModelNullableNonRequired(BaseModel): - int_val: Union[int, None] = None - str_val: Union[str, None] = None - list_val: Union[list[int], None] = None + int_val: int | None = None + str_val: str | None = None + list_val: list[int] | None = None @field_validator("*", mode="before") @classmethod @@ -428,17 +428,17 @@ def test_nullable_non_required_pass_empty_str_to_str_val(path: str): async def read_nullable_with_non_null_default( *, int_val: Annotated[ - Union[int, None], + int | None, Header(), BeforeValidator(lambda v: convert(v)), ] = -1, str_val: Annotated[ - Union[str, None], + str | None, Header(), BeforeValidator(lambda v: convert(v)), ] = "default", list_val: Annotated[ - Union[list[int], None], + list[int] | None, Header(default_factory=lambda: [0]), BeforeValidator(lambda v: convert(v)), ], @@ -452,9 +452,9 @@ async def read_nullable_with_non_null_default( class ModelNullableWithNonNullDefault(BaseModel): - int_val: Union[int, None] = -1 - str_val: Union[str, None] = "default" - list_val: Union[list[int], None] = [0] + int_val: int | None = -1 + str_val: str | None = "default" + list_val: list[int] | None = [0] @field_validator("*", mode="before") @classmethod diff --git a/tests/test_request_params/test_query/test_nullable_and_defaults.py b/tests/test_request_params/test_query/test_nullable_and_defaults.py index 47cb67cb4..648f26580 100644 --- a/tests/test_request_params/test_query/test_nullable_and_defaults.py +++ b/tests/test_request_params/test_query/test_nullable_and_defaults.py @@ -1,4 +1,4 @@ -from typing import Annotated, Any, Union +from typing import Annotated, Any from unittest.mock import Mock, patch import pytest @@ -21,15 +21,15 @@ def convert(v: Any) -> Any: @app.get("/nullable-required") async def read_nullable_required( int_val: Annotated[ - Union[int, None], + int | None, BeforeValidator(lambda v: convert(v)), ], str_val: Annotated[ - Union[str, None], + str | None, BeforeValidator(lambda v: convert(v)), ], list_val: Annotated[ - Union[list[int], None], + list[int] | None, Query(), BeforeValidator(lambda v: convert(v)), ], @@ -43,9 +43,9 @@ async def read_nullable_required( class ModelNullableRequired(BaseModel): - int_val: Union[int, None] - str_val: Union[str, None] - list_val: Union[list[int], None] + int_val: int | None + str_val: str | None + list_val: list[int] | None @field_validator("*", mode="before") @classmethod @@ -187,15 +187,15 @@ def test_nullable_required_pass_value(path: str, values: dict[str, Any]): @app.get("/nullable-non-required") async def read_nullable_non_required( int_val: Annotated[ - Union[int, None], + int | None, BeforeValidator(lambda v: convert(v)), ] = None, str_val: Annotated[ - Union[str, None], + str | None, BeforeValidator(lambda v: convert(v)), ] = None, list_val: Annotated[ - Union[list[int], None], + list[int] | None, Query(), BeforeValidator(lambda v: convert(v)), ] = None, @@ -209,9 +209,9 @@ async def read_nullable_non_required( class ModelNullableNonRequired(BaseModel): - int_val: Union[int, None] = None - str_val: Union[str, None] = None - list_val: Union[list[int], None] = None + int_val: int | None = None + str_val: str | None = None + list_val: list[int] | None = None @field_validator("*", mode="before") @classmethod @@ -341,15 +341,15 @@ def test_nullable_non_required_pass_value(path: str, values: dict[str, Any]): async def read_nullable_with_non_null_default( *, int_val: Annotated[ - Union[int, None], + int | None, BeforeValidator(lambda v: convert(v)), ] = -1, str_val: Annotated[ - Union[str, None], + str | None, BeforeValidator(lambda v: convert(v)), ] = "default", list_val: Annotated[ - Union[list[int], None], + list[int] | None, Query(default_factory=lambda: [0]), BeforeValidator(lambda v: convert(v)), ], @@ -363,9 +363,9 @@ async def read_nullable_with_non_null_default( class ModelNullableWithNonNullDefault(BaseModel): - int_val: Union[int, None] = -1 - str_val: Union[str, None] = "default" - list_val: Union[list[int], None] = [0] + int_val: int | None = -1 + str_val: str | None = "default" + list_val: list[int] | None = [0] @field_validator("*", mode="before") @classmethod