Coverage for tests/pydantic_specific/async/test_cast.py: 100%

63 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-13 05:19 +0000

1from typing import Annotated 1klabmcndoefghij

2 

3import pytest 1klabmcndoefghij

4from annotated_types import Ge 1klabmcndoefghij

5from pydantic import BaseModel, Field 1klabmcndoefghij

6 

7from fast_depends import inject 1klabmcndoefghij

8from fast_depends.exceptions import ValidationError 1klabmcndoefghij

9from tests.marks import pydanticV2 1klabmcndoefghij

10 

11 

12@pytest.mark.anyio 1klabmcndoefghij

13async def test_pydantic_types_casting(): 1klabmcndoefghij

14 class SomeModel(BaseModel): 1klabmcndoefghij

15 field: int 1klabmcndoe

16 

17 @inject 1klabmcndoefghij

18 async def some_func(a: SomeModel): 1klabmcndoefghij

19 return a.field 1klabmcndoefghij

20 

21 assert isinstance(await some_func({"field": "31"}), int) 1klabmcndoefghij

22 

23 

24@pytest.mark.anyio 1klabmcndoefghij

25async def test_pydantic_field_types_casting(): 1klabmcndoefghij

26 @inject 1klabmcndoefghij

27 async def some_func(a: int = Field(..., alias="b")) -> float: 1klabmcndoefghij

28 assert isinstance(a, int) 1klabmcndoefghij

29 return a 1klabmcndoefghij

30 

31 @inject 1klabmcndoefghij

32 async def another_func(a=Field(..., alias="b")) -> float: 1klabmcndoefghij

33 assert isinstance(a, str) 1klabmcndoefghij

34 return a 1klabmcndoefghij

35 

36 assert isinstance(await some_func(b="2", c=3), float) 1klabmcndoefghij

37 assert isinstance(await another_func(b="2"), float) 1klabmcndoefghij

38 

39 

40@pytest.mark.anyio 1klabmcndoefghij

41async def test_annotated(): 1klabmcndoefghij

42 A = Annotated[int, Field(..., alias="b")] 1klabmcndoefghij

43 

44 @inject 1klabmcndoefghij

45 async def some_func(a: A) -> float: 1klabmcndoefghij

46 assert isinstance(a, int) 1klabmcndoefghij

47 return a 1klabmcndoefghij

48 

49 assert isinstance(await some_func(b="2"), float) 1klabmcndoefghij

50 

51 

52@pytest.mark.anyio 1klabmcndoefghij

53async def test_generator(): 1klabmcndoefghij

54 @inject 1klabmcndoefghij

55 async def simple_func(a: str) -> int: 1klabmcndoefghij

56 for _ in range(2): 1klabmcndoefghij

57 yield a 1klabmcndoefghij

58 

59 async for i in simple_func("1"): 1klabmcndoefghij

60 assert i == 1 1klabmcndoefghij

61 

62 

63@pytest.mark.anyio 1klabmcndoefghij

64async def test_validation_error(): 1klabmcndoefghij

65 @inject 1klabmcndoefghij

66 async def some_func(a, b: str = Field(..., max_length=1)): 1klabmcndoefghij

67 return 1 1klabmcndoefghij

68 

69 assert await some_func(1, "a") == 1 1klabmcndoefghij

70 

71 with pytest.raises(ValidationError): 1klabmcndoefghij

72 assert await some_func() 1klabmcndoefghij

73 

74 with pytest.raises(ValidationError): 1klabmcndoefghij

75 assert await some_func(1, "dsdas") 1klabmcndoefghij

76 

77 

78@pytest.mark.anyio 1klabmcndoefghij

79@pydanticV2 1klabmcndoefghij

80async def test_multi_annotated(): 1klabmcndoefghij

81 from pydantic.functional_validators import AfterValidator 1abcdefghij

82 

83 @inject() 1abcdefghij

84 async def f(a: Annotated[int, Ge(10), AfterValidator(lambda x: x + 10)]) -> int: 1abcdefghij

85 return a 1abcdefghij

86 

87 with pytest.raises(ValidationError): 1abcdefghij

88 await f(1) 1abcdefghij

89 

90 assert await f(10) == 20 1abcdefghij