12
12
_T = TypeVar ("_T" )
13
13
_ModelT = TypeVar ("_ModelT" , bound = pydantic .BaseModel )
14
14
15
- # --------------- Pydantic v2 compatibility ---------------
15
+ # --------------- Pydantic v2, v3 compatibility ---------------
16
16
17
17
# Pyright incorrectly reports some of our functions as overriding a method when they don't
18
18
# pyright: reportIncompatibleMethodOverride=false
19
19
20
- PYDANTIC_V2 = pydantic .VERSION .startswith ("2 ." )
20
+ PYDANTIC_V1 = pydantic .VERSION .startswith ("1 ." )
21
21
22
- # v1 re-exports
23
22
if TYPE_CHECKING :
24
23
25
24
def parse_date (value : date | StrBytesIntFloat ) -> date : # noqa: ARG001
@@ -44,90 +43,92 @@ def is_typeddict(type_: type[Any]) -> bool: # noqa: ARG001
44
43
...
45
44
46
45
else :
47
- if PYDANTIC_V2 :
48
- from pydantic .v1 .typing import (
46
+ # v1 re-exports
47
+ if PYDANTIC_V1 :
48
+ from pydantic .typing import (
49
49
get_args as get_args ,
50
50
is_union as is_union ,
51
51
get_origin as get_origin ,
52
52
is_typeddict as is_typeddict ,
53
53
is_literal_type as is_literal_type ,
54
54
)
55
- from pydantic .v1 . datetime_parse import parse_date as parse_date , parse_datetime as parse_datetime
55
+ from pydantic .datetime_parse import parse_date as parse_date , parse_datetime as parse_datetime
56
56
else :
57
- from pydantic . typing import (
57
+ from . _utils import (
58
58
get_args as get_args ,
59
59
is_union as is_union ,
60
60
get_origin as get_origin ,
61
+ parse_date as parse_date ,
61
62
is_typeddict as is_typeddict ,
63
+ parse_datetime as parse_datetime ,
62
64
is_literal_type as is_literal_type ,
63
65
)
64
- from pydantic .datetime_parse import parse_date as parse_date , parse_datetime as parse_datetime
65
66
66
67
67
68
# refactored config
68
69
if TYPE_CHECKING :
69
70
from pydantic import ConfigDict as ConfigDict
70
71
else :
71
- if PYDANTIC_V2 :
72
- from pydantic import ConfigDict
73
- else :
72
+ if PYDANTIC_V1 :
74
73
# TODO: provide an error message here?
75
74
ConfigDict = None
75
+ else :
76
+ from pydantic import ConfigDict as ConfigDict
76
77
77
78
78
79
# renamed methods / properties
79
80
def parse_obj (model : type [_ModelT ], value : object ) -> _ModelT :
80
- if PYDANTIC_V2 :
81
- return model .model_validate (value )
82
- else :
81
+ if PYDANTIC_V1 :
83
82
return cast (_ModelT , model .parse_obj (value )) # pyright: ignore[reportDeprecated, reportUnnecessaryCast]
83
+ else :
84
+ return model .model_validate (value )
84
85
85
86
86
87
def field_is_required (field : FieldInfo ) -> bool :
87
- if PYDANTIC_V2 :
88
- return field .is_required ()
89
- return field .required # type: ignore
88
+ if PYDANTIC_V1 :
89
+ return field .required # type: ignore
90
+ return field .is_required ()
90
91
91
92
92
93
def field_get_default (field : FieldInfo ) -> Any :
93
94
value = field .get_default ()
94
- if PYDANTIC_V2 :
95
- from pydantic_core import PydanticUndefined
96
-
97
- if value == PydanticUndefined :
98
- return None
95
+ if PYDANTIC_V1 :
99
96
return value
97
+ from pydantic_core import PydanticUndefined
98
+
99
+ if value == PydanticUndefined :
100
+ return None
100
101
return value
101
102
102
103
103
104
def field_outer_type (field : FieldInfo ) -> Any :
104
- if PYDANTIC_V2 :
105
- return field .annotation
106
- return field .outer_type_ # type: ignore
105
+ if PYDANTIC_V1 :
106
+ return field .outer_type_ # type: ignore
107
+ return field .annotation
107
108
108
109
109
110
def get_model_config (model : type [pydantic .BaseModel ]) -> Any :
110
- if PYDANTIC_V2 :
111
- return model .model_config
112
- return model .__config__ # type: ignore
111
+ if PYDANTIC_V1 :
112
+ return model .__config__ # type: ignore
113
+ return model .model_config
113
114
114
115
115
116
def get_model_fields (model : type [pydantic .BaseModel ]) -> dict [str , FieldInfo ]:
116
- if PYDANTIC_V2 :
117
- return model .model_fields
118
- return model .__fields__ # type: ignore
117
+ if PYDANTIC_V1 :
118
+ return model .__fields__ # type: ignore
119
+ return model .model_fields
119
120
120
121
121
122
def model_copy (model : _ModelT , * , deep : bool = False ) -> _ModelT :
122
- if PYDANTIC_V2 :
123
- return model .model_copy (deep = deep )
124
- return model .copy (deep = deep ) # type: ignore
123
+ if PYDANTIC_V1 :
124
+ return model .copy (deep = deep ) # type: ignore
125
+ return model .model_copy (deep = deep )
125
126
126
127
127
128
def model_json (model : pydantic .BaseModel , * , indent : int | None = None ) -> str :
128
- if PYDANTIC_V2 :
129
- return model .model_dump_json (indent = indent )
130
- return model .json (indent = indent ) # type: ignore
129
+ if PYDANTIC_V1 :
130
+ return model .json (indent = indent ) # type: ignore
131
+ return model .model_dump_json (indent = indent )
131
132
132
133
133
134
def model_dump (
@@ -139,14 +140,14 @@ def model_dump(
139
140
warnings : bool = True ,
140
141
mode : Literal ["json" , "python" ] = "python" ,
141
142
) -> dict [str , Any ]:
142
- if PYDANTIC_V2 or hasattr (model , "model_dump" ):
143
+ if ( not PYDANTIC_V1 ) or hasattr (model , "model_dump" ):
143
144
return model .model_dump (
144
145
mode = mode ,
145
146
exclude = exclude ,
146
147
exclude_unset = exclude_unset ,
147
148
exclude_defaults = exclude_defaults ,
148
149
# warnings are not supported in Pydantic v1
149
- warnings = warnings if PYDANTIC_V2 else True ,
150
+ warnings = True if PYDANTIC_V1 else warnings ,
150
151
)
151
152
return cast (
152
153
"dict[str, Any]" ,
@@ -159,9 +160,9 @@ def model_dump(
159
160
160
161
161
162
def model_parse (model : type [_ModelT ], data : Any ) -> _ModelT :
162
- if PYDANTIC_V2 :
163
- return model .model_validate (data )
164
- return model .parse_obj (data ) # pyright: ignore[reportDeprecated]
163
+ if PYDANTIC_V1 :
164
+ return model .parse_obj (data ) # pyright: ignore[reportDeprecated]
165
+ return model .model_validate (data )
165
166
166
167
167
168
# generic models
@@ -170,17 +171,16 @@ def model_parse(model: type[_ModelT], data: Any) -> _ModelT:
170
171
class GenericModel (pydantic .BaseModel ): ...
171
172
172
173
else :
173
- if PYDANTIC_V2 :
174
+ if PYDANTIC_V1 :
175
+ import pydantic .generics
176
+
177
+ class GenericModel (pydantic .generics .GenericModel , pydantic .BaseModel ): ...
178
+ else :
174
179
# there no longer needs to be a distinction in v2 but
175
180
# we still have to create our own subclass to avoid
176
181
# inconsistent MRO ordering errors
177
182
class GenericModel (pydantic .BaseModel ): ...
178
183
179
- else :
180
- import pydantic .generics
181
-
182
- class GenericModel (pydantic .generics .GenericModel , pydantic .BaseModel ): ...
183
-
184
184
185
185
# cached properties
186
186
if TYPE_CHECKING :
0 commit comments