@@ -44,6 +44,13 @@ class PydanticValidationError(Exception): # type: ignore
44
44
45
45
try :
46
46
from attrs import fields as attrs_fields , has as is_attrs
47
+ except ImportError :
48
+
49
+ def is_attrs (object_ : Any ) -> bool : # type: ignore
50
+ return False
51
+
52
+
53
+ try :
47
54
from msgspec import convert , Struct , to_builtins , ValidationError as MsgSpecValidationError
48
55
from msgspec .json import schema_components
49
56
except ImportError :
@@ -52,9 +59,6 @@ class PydanticValidationError(Exception): # type: ignore
52
59
class Struct : # type: ignore
53
60
pass
54
61
55
- def is_attrs (object_ : Any ) -> bool : # type: ignore
56
- return False
57
-
58
62
def convert (object_ : Any , type_ : Any ) -> Any : # type: ignore
59
63
raise RuntimeError ("Cannot convert, msgspec not installed" )
60
64
@@ -244,24 +248,21 @@ def _is_list_or_dict(type_: Type) -> bool:
244
248
245
249
246
250
def _use_pydantic (model_class : Type , preference : Optional [str ]) -> bool :
247
- return (
251
+ return PYDANTIC_INSTALLED and (
248
252
is_pydantic_dataclass (model_class )
249
253
or (isclass (model_class ) and issubclass (model_class , BaseModel ))
250
254
or (
251
- (_is_list_or_dict (model_class ) or is_dataclass (model_class ))
252
- and PYDANTIC_INSTALLED
253
- and preference != "msgspec"
255
+ (_is_list_or_dict (model_class ) or is_dataclass (model_class )) and preference != "msgspec"
254
256
)
255
257
)
256
258
257
259
258
260
def _use_msgspec (model_class : Type , preference : Optional [str ]) -> bool :
259
- return (
261
+ return MSGSPEC_INSTALLED and (
260
262
(isclass (model_class ) and issubclass (model_class , Struct ))
261
263
or is_attrs (model_class )
262
264
or (
263
265
(_is_list_or_dict (model_class ) or is_dataclass (model_class ))
264
- and MSGSPEC_INSTALLED
265
266
and preference != "pydantic"
266
267
)
267
268
)
0 commit comments