From 0acd31576ba172c71ddaeb581e46d923e29d081f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Ram=C3=ADrez?= Date: Sun, 21 Dec 2025 16:31:11 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20Add=20test=20for=20warnings?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../test_pydantic_v1_deprecation_warnings.py | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 tests/test_pydantic_v1_deprecation_warnings.py diff --git a/tests/test_pydantic_v1_deprecation_warnings.py b/tests/test_pydantic_v1_deprecation_warnings.py new file mode 100644 index 000000000..00694c906 --- /dev/null +++ b/tests/test_pydantic_v1_deprecation_warnings.py @@ -0,0 +1,77 @@ +import sys + +import pytest + +from tests.utils import skip_module_if_py_gte_314 + +if sys.version_info >= (3, 14): + skip_module_if_py_gte_314() + +from fastapi import FastAPI +from fastapi._compat.v1 import BaseModel + + +def test_warns_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:", + ): + + @app.post("/param") + def endpoint(data: ParamModelV1): + return data + + +def test_warns_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", + ): + + @app.get("/return") + def endpoint() -> ReturnModelV1: + return ReturnModelV1(name="test") + + +def test_warns_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", + ): + + @app.get("/response-model", response_model=ResponseModelV1) + def endpoint(): + return {"name": "test"} + + +def test_warns_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", + ): + + @app.get( + "/responses", response_model=None, responses={400: {"model": ErrorModelV1}} + ) + def endpoint(): + return {"ok": True}