From fd58f903697d56a8f121e22e8509dbbce942d89b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Ram=C3=ADrez?= Date: Thu, 25 Dec 2025 12:06:27 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20Update=20tests=20for=20pydantic.v1?= =?UTF-8?q?=20to=20ensure=20error?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../test_pydantic_v1_deprecation_warnings.py | 58 +++++-------------- 1 file changed, 13 insertions(+), 45 deletions(-) diff --git a/tests/test_pydantic_v1_deprecation_warnings.py b/tests/test_pydantic_v1_deprecation_warnings.py index e0008e218..89f20cafb 100644 --- a/tests/test_pydantic_v1_deprecation_warnings.py +++ b/tests/test_pydantic_v1_deprecation_warnings.py @@ -9,90 +9,58 @@ if sys.version_info >= (3, 14): from fastapi import FastAPI from fastapi._compat.v1 import BaseModel -from fastapi.testclient import TestClient +from fastapi.exceptions import PydanticV1NotSupportedError -def test_warns_pydantic_v1_model_in_endpoint_param() -> None: +def test_raises_pydantic_v1_model_in_endpoint_param() -> None: class ParamModelV1(BaseModel): name: str app = FastAPI() - with pytest.warns( - DeprecationWarning, - match=r"pydantic\.v1 is deprecated.*Please update the param data:", - ): + with pytest.raises(PydanticV1NotSupportedError): @app.post("/param") - def endpoint(data: ParamModelV1): + def endpoint(data: ParamModelV1): # pragma: no cover return data - client = TestClient(app) - response = client.post("/param", json={"name": "test"}) - assert response.status_code == 200, response.text - assert response.json() == {"name": "test"} - -def test_warns_pydantic_v1_model_in_return_type() -> None: +def test_raises_pydantic_v1_model_in_return_type() -> None: class ReturnModelV1(BaseModel): name: str app = FastAPI() - with pytest.warns( - DeprecationWarning, - match=r"pydantic\.v1 is deprecated.*Please update the response model", - ): + with pytest.raises(PydanticV1NotSupportedError): @app.get("/return") - def endpoint() -> ReturnModelV1: + def endpoint() -> ReturnModelV1: # pragma: no cover return ReturnModelV1(name="test") - client = TestClient(app) - response = client.get("/return") - assert response.status_code == 200, response.text - assert response.json() == {"name": "test"} - -def test_warns_pydantic_v1_model_in_response_model() -> None: +def test_raises_pydantic_v1_model_in_response_model() -> None: class ResponseModelV1(BaseModel): name: str app = FastAPI() - with pytest.warns( - DeprecationWarning, - match=r"pydantic\.v1 is deprecated.*Please update the response model", - ): + with pytest.raises(PydanticV1NotSupportedError): @app.get("/response-model", response_model=ResponseModelV1) - def endpoint(): + def endpoint(): # pragma: no cover return {"name": "test"} - client = TestClient(app) - response = client.get("/response-model") - assert response.status_code == 200, response.text - assert response.json() == {"name": "test"} - -def test_warns_pydantic_v1_model_in_additional_responses_model() -> None: +def test_raises_pydantic_v1_model_in_additional_responses_model() -> None: class ErrorModelV1(BaseModel): detail: str app = FastAPI() - with pytest.warns( - DeprecationWarning, - match=r"pydantic\.v1 is deprecated.*In responses=\{\}, please update", - ): + with pytest.raises(PydanticV1NotSupportedError): @app.get( "/responses", response_model=None, responses={400: {"model": ErrorModelV1}} ) - def endpoint(): + def endpoint(): # pragma: no cover return {"ok": True} - - client = TestClient(app) - response = client.get("/responses") - assert response.status_code == 200, response.text - assert response.json() == {"ok": True}