Browse Source

Add type annotations to pydanic model tests

This will allow mypy to check the return type of
`ValidationException.errors()` in a later commit.
pull/11542/head
Tamir Duberstein 1 week ago
parent
commit
61ceeb129b
Failed to extract signature
  1. 8
      tests/test_filter_pydantic_sub_model/app_pv1.py
  2. 8
      tests/test_filter_pydantic_sub_model/test_filter_pydantic_sub_model_pv1.py
  3. 16
      tests/test_filter_pydantic_sub_model_pv2.py

8
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}

8
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() == {

16
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() == {

Loading…
Cancel
Save