Coverage for pydantic/warnings.py: 96.43%

26 statements  

« prev     ^ index     » next       coverage.py v7.5.4, created at 2024-07-03 19:29 +0000

1"""Pydantic-specific warnings.""" 

2 

3from __future__ import annotations as _annotations 1abcdefghijklmnopqrstuvwxyzMNOPQRSTUVABCDEFGHIJKL

4 

5from .version import version_short 1abcdefghijklmnopqrstuvwxyzMNOPQRSTUVABCDEFGHIJKL

6 

7__all__ = ( 1abcdefghijklmnopqrstuvwxyzMNOPQRSTUVABCDEFGHIJKL

8 'PydanticDeprecatedSince20', 

9 'PydanticDeprecationWarning', 

10 'PydanticDeprecatedSince26', 

11 'PydanticExperimentalWarning', 

12) 

13 

14 

15class PydanticDeprecationWarning(DeprecationWarning): 1abcdefghijklmnopqrstuvwxyzMNOPQRSTUVABCDEFGHIJKL

16 """A Pydantic specific deprecation warning. 

17 

18 This warning is raised when using deprecated functionality in Pydantic. It provides information on when the 

19 deprecation was introduced and the expected version in which the corresponding functionality will be removed. 

20 

21 Attributes: 

22 message: Description of the warning. 

23 since: Pydantic version in what the deprecation was introduced. 

24 expected_removal: Pydantic version in what the corresponding functionality expected to be removed. 

25 """ 

26 

27 message: str 1abcdefghijklmnopqrstuvwxyzMNOPQRSTUVABCDEFGHIJKL

28 since: tuple[int, int] 1abcdefghijklmnopqrstuvwxyzMNOPQRSTUVABCDEFGHIJKL

29 expected_removal: tuple[int, int] 1abcdefghijklmnopqrstuvwxyzMNOPQRSTUVABCDEFGHIJKL

30 

31 def __init__( 1abcdefghijklmnopqrstuvwxyzMNOPQRSTUVABCDEFGHIJKL

32 self, message: str, *args: object, since: tuple[int, int], expected_removal: tuple[int, int] | None = None 

33 ) -> None: 

34 super().__init__(message, *args) 1abcdefghijklmnopqrstuvwxyzABCDEFGHIJKL

35 self.message = message.rstrip('.') 1abcdefghijklmnopqrstuvwxyzABCDEFGHIJKL

36 self.since = since 1abcdefghijklmnopqrstuvwxyzABCDEFGHIJKL

37 self.expected_removal = expected_removal if expected_removal is not None else (since[0] + 1, 0) 1abcdefghijklmnopqrstuvwxyzABCDEFGHIJKL

38 

39 def __str__(self) -> str: 1abcdefghijklmnopqrstuvwxyzMNOPQRSTUVABCDEFGHIJKL

40 message = ( 1abcdefghijklmnopqrstuvwxyzABCDEFGHIJKL

41 f'{self.message}. Deprecated in Pydantic V{self.since[0]}.{self.since[1]}' 

42 f' to be removed in V{self.expected_removal[0]}.{self.expected_removal[1]}.' 

43 ) 

44 if self.since == (2, 0): 1abcdefghijklmnopqrstuvwxyzABCDEFGHIJKL

45 message += f' See Pydantic V2 Migration Guide at https://errors.pydantic.dev/{version_short()}/migration/' 1abcdefghijklmnopqrstuvwxyzABCDEFGHIJKL

46 return message 1abcdefghijklmnopqrstuvwxyzABCDEFGHIJKL

47 

48 

49class PydanticDeprecatedSince20(PydanticDeprecationWarning): 1abcdefghijklmnopqrstuvwxyzMNOPQRSTUVABCDEFGHIJKL

50 """A specific `PydanticDeprecationWarning` subclass defining functionality deprecated since Pydantic 2.0.""" 

51 

52 def __init__(self, message: str, *args: object) -> None: 1abcdefghijklmnopqrstuvwxyzMNOPQRSTUVABCDEFGHIJKL

53 super().__init__(message, *args, since=(2, 0), expected_removal=(3, 0)) 1abcdefghijklmnopqrstuvwxyzABCDEFGHIJKL

54 

55 

56class PydanticDeprecatedSince26(PydanticDeprecationWarning): 1abcdefghijklmnopqrstuvwxyzMNOPQRSTUVABCDEFGHIJKL

57 """A specific `PydanticDeprecationWarning` subclass defining functionality deprecated since Pydantic 2.6.""" 

58 

59 def __init__(self, message: str, *args: object) -> None: 1abcdefghijklmnopqrstuvwxyzMNOPQRSTUVABCDEFGHIJKL

60 super().__init__(message, *args, since=(2, 0), expected_removal=(3, 0)) 

61 

62 

63class GenericBeforeBaseModelWarning(Warning): 1abcdefghijklmnopqrstuvwxyzMNOPQRSTUVABCDEFGHIJKL

64 pass 1abcdefghijklmnopqrstuvwxyzMNOPQRSTUVABCDEFGHIJKL

65 

66 

67class PydanticExperimentalWarning(Warning): 1abcdefghijklmnopqrstuvwxyzMNOPQRSTUVABCDEFGHIJKL

68 """A Pydantic specific experimental functionality warning. 

69 

70 This warning is raised when using experimental functionality in Pydantic. 

71 It is raised to warn users that the functionality may change or be removed in future versions of Pydantic. 

72 """