1"""Pydantic-specific warnings."""
2
3from __future__ import annotations as _annotations
4
5from .version import version_short
6
7__all__ = (
8 'PydanticDeprecatedSince20',
9 'PydanticDeprecatedSince26',
10 'PydanticDeprecatedSince29',
11 'PydanticDeprecatedSince210',
12 'PydanticDeprecatedSince211',
13 'PydanticDeprecationWarning',
14 'PydanticExperimentalWarning',
15 'ArbitraryTypeWarning',
16 'UnsupportedFieldAttributeWarning',
17 'TypedDictExtraConfigWarning',
18)
19
20
21class PydanticDeprecationWarning(DeprecationWarning):
22 """A Pydantic specific deprecation warning.
23
24 This warning is raised when using deprecated functionality in Pydantic. It provides information on when the
25 deprecation was introduced and the expected version in which the corresponding functionality will be removed.
26
27 Attributes:
28 message: Description of the warning.
29 since: Pydantic version in what the deprecation was introduced.
30 expected_removal: Pydantic version in what the corresponding functionality expected to be removed.
31 """
32
33 message: str
34 since: tuple[int, int]
35 expected_removal: tuple[int, int]
36
37 def __init__(
38 self, message: str, *args: object, since: tuple[int, int], expected_removal: tuple[int, int] | None = None
39 ) -> None:
40 super().__init__(message, *args)
41 self.message = message.rstrip('.')
42 self.since = since
43 self.expected_removal = expected_removal if expected_removal is not None else (since[0] + 1, 0)
44
45 def __str__(self) -> str:
46 message = (
47 f'{self.message}. Deprecated in Pydantic V{self.since[0]}.{self.since[1]}'
48 f' to be removed in V{self.expected_removal[0]}.{self.expected_removal[1]}.'
49 )
50 if self.since == (2, 0):
51 message += f' See Pydantic V2 Migration Guide at https://errors.pydantic.dev/{version_short()}/migration/'
52 return message
53
54
55class PydanticDeprecatedSince20(PydanticDeprecationWarning):
56 """A specific `PydanticDeprecationWarning` subclass defining functionality deprecated since Pydantic 2.0."""
57
58 def __init__(self, message: str, *args: object) -> None:
59 super().__init__(message, *args, since=(2, 0), expected_removal=(3, 0))
60
61
62class PydanticDeprecatedSince26(PydanticDeprecationWarning):
63 """A specific `PydanticDeprecationWarning` subclass defining functionality deprecated since Pydantic 2.6."""
64
65 def __init__(self, message: str, *args: object) -> None:
66 super().__init__(message, *args, since=(2, 6), expected_removal=(3, 0))
67
68
69class PydanticDeprecatedSince29(PydanticDeprecationWarning):
70 """A specific `PydanticDeprecationWarning` subclass defining functionality deprecated since Pydantic 2.9."""
71
72 def __init__(self, message: str, *args: object) -> None:
73 super().__init__(message, *args, since=(2, 9), expected_removal=(3, 0))
74
75
76class PydanticDeprecatedSince210(PydanticDeprecationWarning):
77 """A specific `PydanticDeprecationWarning` subclass defining functionality deprecated since Pydantic 2.10."""
78
79 def __init__(self, message: str, *args: object) -> None:
80 super().__init__(message, *args, since=(2, 10), expected_removal=(3, 0))
81
82
83class PydanticDeprecatedSince211(PydanticDeprecationWarning):
84 """A specific `PydanticDeprecationWarning` subclass defining functionality deprecated since Pydantic 2.11."""
85
86 def __init__(self, message: str, *args: object) -> None:
87 super().__init__(message, *args, since=(2, 11), expected_removal=(3, 0))
88
89
90class GenericBeforeBaseModelWarning(Warning):
91 pass
92
93
94class PydanticExperimentalWarning(Warning):
95 """A Pydantic specific experimental functionality warning.
96
97 It is raised to warn users that the functionality may change or be removed in future versions of Pydantic.
98 """
99
100
101class CoreSchemaGenerationWarning(UserWarning):
102 """A warning raised during core schema generation."""
103
104
105class ArbitraryTypeWarning(CoreSchemaGenerationWarning):
106 """A warning raised when Pydantic fails to generate a core schema for an arbitrary type."""
107
108
109class UnsupportedFieldAttributeWarning(CoreSchemaGenerationWarning):
110 """A warning raised when a `Field()` attribute isn't supported in the context it is used."""
111
112
113class TypedDictExtraConfigWarning(CoreSchemaGenerationWarning):
114 """A warning raised when the [`extra`][pydantic.ConfigDict.extra] configuration is incompatible with the `closed` or `extra_items` specification."""