Coverage for tests / test_pydantic_v1_error.py: 100%
42 statements
« prev ^ index » next coverage.py v7.13.3, created at 2026-02-12 18:15 +0000
« prev ^ index » next coverage.py v7.13.3, created at 2026-02-12 18:15 +0000
1import sys 1adbc
2import warnings 1adbc
3from typing import Union 1adbc
5import pytest 1adbc
7from tests.utils import skip_module_if_py_gte_314 1adbc
9if sys.version_info >= (3, 14): 1adbc
10 skip_module_if_py_gte_314() 1d
12from fastapi import FastAPI 1abc
13from fastapi.exceptions import PydanticV1NotSupportedError 1abc
15with warnings.catch_warnings(): 1abc
16 warnings.simplefilter("ignore", UserWarning) 1abc
17 from pydantic.v1 import BaseModel 1abc
20def test_raises_pydantic_v1_model_in_endpoint_param() -> None: 1abc
21 class ParamModelV1(BaseModel): 1gh
22 name: str 1gh
24 app = FastAPI() 1gh
26 with pytest.raises(PydanticV1NotSupportedError): 1gh
28 @app.post("/param") 1gh
29 def endpoint(data: ParamModelV1): # pragma: no cover 1gh
30 return data
33def test_raises_pydantic_v1_model_in_return_type() -> None: 1abc
34 class ReturnModelV1(BaseModel): 1ij
35 name: str 1ij
37 app = FastAPI() 1ij
39 with pytest.raises(PydanticV1NotSupportedError): 1ij
41 @app.get("/return") 1ij
42 def endpoint() -> ReturnModelV1: # pragma: no cover 1ij
43 return ReturnModelV1(name="test")
46def test_raises_pydantic_v1_model_in_response_model() -> None: 1abc
47 class ResponseModelV1(BaseModel): 1kl
48 name: str 1kl
50 app = FastAPI() 1kl
52 with pytest.raises(PydanticV1NotSupportedError): 1kl
54 @app.get("/response-model", response_model=ResponseModelV1) 1kl
55 def endpoint(): # pragma: no cover 1kl
56 return {"name": "test"}
59def test_raises_pydantic_v1_model_in_additional_responses_model() -> None: 1abc
60 class ErrorModelV1(BaseModel): 1ef
61 detail: str 1ef
63 app = FastAPI() 1ef
65 with pytest.raises(PydanticV1NotSupportedError): 1ef
67 @app.get( 1ef
68 "/responses", response_model=None, responses={400: {"model": ErrorModelV1}} 1ef
69 )
70 def endpoint(): # pragma: no cover 1ef
71 return {"ok": True}
74def test_raises_pydantic_v1_model_in_union() -> None: 1abc
75 class ModelV1A(BaseModel): 1mn
76 name: str 1mn
78 app = FastAPI() 1mn
80 with pytest.raises(PydanticV1NotSupportedError): 1mn
82 @app.post("/union") 1mn
83 def endpoint(data: Union[dict, ModelV1A]): # pragma: no cover 1mn
84 return data
87def test_raises_pydantic_v1_model_in_sequence() -> None: 1abc
88 class ModelV1A(BaseModel): 1op
89 name: str 1op
91 app = FastAPI() 1op
93 with pytest.raises(PydanticV1NotSupportedError): 1op
95 @app.post("/sequence") 1op
96 def endpoint(data: list[ModelV1A]): # pragma: no cover 1op
97 return data