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

1import sys 1adbc

2import warnings 1adbc

3from typing import Union 1adbc

4 

5import pytest 1adbc

6 

7from tests.utils import skip_module_if_py_gte_314 1adbc

8 

9if sys.version_info >= (3, 14): 1adbc

10 skip_module_if_py_gte_314() 1d

11 

12from fastapi import FastAPI 1abc

13from fastapi.exceptions import PydanticV1NotSupportedError 1abc

14 

15with warnings.catch_warnings(): 1abc

16 warnings.simplefilter("ignore", UserWarning) 1abc

17 from pydantic.v1 import BaseModel 1abc

18 

19 

20def test_raises_pydantic_v1_model_in_endpoint_param() -> None: 1abc

21 class ParamModelV1(BaseModel): 1gh

22 name: str 1gh

23 

24 app = FastAPI() 1gh

25 

26 with pytest.raises(PydanticV1NotSupportedError): 1gh

27 

28 @app.post("/param") 1gh

29 def endpoint(data: ParamModelV1): # pragma: no cover 1gh

30 return data 

31 

32 

33def test_raises_pydantic_v1_model_in_return_type() -> None: 1abc

34 class ReturnModelV1(BaseModel): 1ij

35 name: str 1ij

36 

37 app = FastAPI() 1ij

38 

39 with pytest.raises(PydanticV1NotSupportedError): 1ij

40 

41 @app.get("/return") 1ij

42 def endpoint() -> ReturnModelV1: # pragma: no cover 1ij

43 return ReturnModelV1(name="test") 

44 

45 

46def test_raises_pydantic_v1_model_in_response_model() -> None: 1abc

47 class ResponseModelV1(BaseModel): 1kl

48 name: str 1kl

49 

50 app = FastAPI() 1kl

51 

52 with pytest.raises(PydanticV1NotSupportedError): 1kl

53 

54 @app.get("/response-model", response_model=ResponseModelV1) 1kl

55 def endpoint(): # pragma: no cover 1kl

56 return {"name": "test"} 

57 

58 

59def test_raises_pydantic_v1_model_in_additional_responses_model() -> None: 1abc

60 class ErrorModelV1(BaseModel): 1ef

61 detail: str 1ef

62 

63 app = FastAPI() 1ef

64 

65 with pytest.raises(PydanticV1NotSupportedError): 1ef

66 

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} 

72 

73 

74def test_raises_pydantic_v1_model_in_union() -> None: 1abc

75 class ModelV1A(BaseModel): 1mn

76 name: str 1mn

77 

78 app = FastAPI() 1mn

79 

80 with pytest.raises(PydanticV1NotSupportedError): 1mn

81 

82 @app.post("/union") 1mn

83 def endpoint(data: Union[dict, ModelV1A]): # pragma: no cover 1mn

84 return data 

85 

86 

87def test_raises_pydantic_v1_model_in_sequence() -> None: 1abc

88 class ModelV1A(BaseModel): 1op

89 name: str 1op

90 

91 app = FastAPI() 1op

92 

93 with pytest.raises(PydanticV1NotSupportedError): 1op

94 

95 @app.post("/sequence") 1op

96 def endpoint(data: list[ModelV1A]): # pragma: no cover 1op

97 return data