From 61ceeb129b88b2a5dd0100f08f865255c0db89d4 Mon Sep 17 00:00:00 2001 From: Tamir Duberstein Date: Wed, 23 Jul 2025 10:05:55 -0400 Subject: [PATCH] Add type annotations to pydanic model tests This will allow mypy to check the return type of `ValidationException.errors()` in a later commit. --- tests/test_filter_pydantic_sub_model/app_pv1.py | 8 +++++--- .../test_filter_pydantic_sub_model_pv1.py | 8 ++++---- tests/test_filter_pydantic_sub_model_pv2.py | 16 +++++++++------- 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/tests/test_filter_pydantic_sub_model/app_pv1.py b/tests/test_filter_pydantic_sub_model/app_pv1.py index 657e8c5d1..ab3bf87a8 100644 --- a/tests/test_filter_pydantic_sub_model/app_pv1.py +++ b/tests/test_filter_pydantic_sub_model/app_pv1.py @@ -1,4 +1,4 @@ -from typing import Optional +from typing import Any, Dict, Optional from fastapi import Depends, FastAPI from pydantic import BaseModel, validator @@ -20,7 +20,7 @@ class ModelA(BaseModel): model_b: ModelB @validator("name") - def lower_username(cls, name: str, values): + def lower_username(cls, name: str, values: Dict[str, Any]) -> str: if not name.endswith("A"): raise ValueError("name must end in A") return name @@ -31,5 +31,7 @@ async def get_model_c() -> ModelC: @app.get("/model/{name}", response_model=ModelA) -async def get_model_a(name: str, model_c=Depends(get_model_c)): +async def get_model_a( + name: str, model_c: ModelC = Depends(get_model_c) +) -> Dict[str, Any]: return {"name": name, "description": "model-a-desc", "model_b": model_c} diff --git a/tests/test_filter_pydantic_sub_model/test_filter_pydantic_sub_model_pv1.py b/tests/test_filter_pydantic_sub_model/test_filter_pydantic_sub_model_pv1.py index 48732dbf0..cff505ca8 100644 --- a/tests/test_filter_pydantic_sub_model/test_filter_pydantic_sub_model_pv1.py +++ b/tests/test_filter_pydantic_sub_model/test_filter_pydantic_sub_model_pv1.py @@ -6,7 +6,7 @@ from ..utils import needs_pydanticv1 @pytest.fixture(name="client") -def get_client(): +def get_client() -> TestClient: from .app_pv1 import app client = TestClient(app) @@ -14,7 +14,7 @@ def get_client(): @needs_pydanticv1 -def test_filter_sub_model(client: TestClient): +def test_filter_sub_model(client: TestClient) -> None: response = client.get("/model/modelA") assert response.status_code == 200, response.text assert response.json() == { @@ -25,7 +25,7 @@ def test_filter_sub_model(client: TestClient): @needs_pydanticv1 -def test_validator_is_cloned(client: TestClient): +def test_validator_is_cloned(client: TestClient) -> None: with pytest.raises(ResponseValidationError) as err: client.get("/model/modelX") assert err.value.errors() == [ @@ -38,7 +38,7 @@ def test_validator_is_cloned(client: TestClient): @needs_pydanticv1 -def test_openapi_schema(client: TestClient): +def test_openapi_schema(client: TestClient) -> None: response = client.get("/openapi.json") assert response.status_code == 200, response.text assert response.json() == { diff --git a/tests/test_filter_pydantic_sub_model_pv2.py b/tests/test_filter_pydantic_sub_model_pv2.py index 185db2b3c..b8116f4ea 100644 --- a/tests/test_filter_pydantic_sub_model_pv2.py +++ b/tests/test_filter_pydantic_sub_model_pv2.py @@ -1,4 +1,4 @@ -from typing import Optional +from typing import Any, Dict, Optional import pytest from dirty_equals import HasRepr, IsDict @@ -10,7 +10,7 @@ from .utils import needs_pydanticv2 @pytest.fixture(name="client") -def get_client(): +def get_client() -> TestClient: from pydantic import BaseModel, ValidationInfo, field_validator app = FastAPI() @@ -27,7 +27,7 @@ def get_client(): foo: ModelB @field_validator("name") - def lower_username(cls, name: str, info: ValidationInfo): + def lower_username(cls, name: str, info: ValidationInfo) -> str: if not name.endswith("A"): raise ValueError("name must end in A") return name @@ -36,7 +36,9 @@ def get_client(): return ModelC(username="test-user", password="test-password") @app.get("/model/{name}", response_model=ModelA) - async def get_model_a(name: str, model_c=Depends(get_model_c)): + async def get_model_a( + name: str, model_c: ModelC = Depends(get_model_c) + ) -> Dict[str, Any]: return {"name": name, "description": "model-a-desc", "foo": model_c} client = TestClient(app) @@ -44,7 +46,7 @@ def get_client(): @needs_pydanticv2 -def test_filter_sub_model(client: TestClient): +def test_filter_sub_model(client: TestClient) -> None: response = client.get("/model/modelA") assert response.status_code == 200, response.text assert response.json() == { @@ -55,7 +57,7 @@ def test_filter_sub_model(client: TestClient): @needs_pydanticv2 -def test_validator_is_cloned(client: TestClient): +def test_validator_is_cloned(client: TestClient) -> None: with pytest.raises(ResponseValidationError) as err: client.get("/model/modelX") assert err.value.errors() == [ @@ -72,7 +74,7 @@ def test_validator_is_cloned(client: TestClient): @needs_pydanticv2 -def test_openapi_schema(client: TestClient): +def test_openapi_schema(client: TestClient) -> None: response = client.get("/openapi.json") assert response.status_code == 200, response.text assert response.json() == {