Skip to content

Commit 00a596b

Browse files
committed
Bugfix using msgspec without attrs
If msgspec is installed but attrs isn't quart-schema should still work for msgspec objects which it didn't as the import would fail.
1 parent 1e9d3e8 commit 00a596b

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

src/quart_schema/conversion.py

+10-9
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ class PydanticValidationError(Exception): # type: ignore
4444

4545
try:
4646
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:
4754
from msgspec import convert, Struct, to_builtins, ValidationError as MsgSpecValidationError
4855
from msgspec.json import schema_components
4956
except ImportError:
@@ -52,9 +59,6 @@ class PydanticValidationError(Exception): # type: ignore
5259
class Struct: # type: ignore
5360
pass
5461

55-
def is_attrs(object_: Any) -> bool: # type: ignore
56-
return False
57-
5862
def convert(object_: Any, type_: Any) -> Any: # type: ignore
5963
raise RuntimeError("Cannot convert, msgspec not installed")
6064

@@ -244,24 +248,21 @@ def _is_list_or_dict(type_: Type) -> bool:
244248

245249

246250
def _use_pydantic(model_class: Type, preference: Optional[str]) -> bool:
247-
return (
251+
return PYDANTIC_INSTALLED and (
248252
is_pydantic_dataclass(model_class)
249253
or (isclass(model_class) and issubclass(model_class, BaseModel))
250254
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"
254256
)
255257
)
256258

257259

258260
def _use_msgspec(model_class: Type, preference: Optional[str]) -> bool:
259-
return (
261+
return MSGSPEC_INSTALLED and (
260262
(isclass(model_class) and issubclass(model_class, Struct))
261263
or is_attrs(model_class)
262264
or (
263265
(_is_list_or_dict(model_class) or is_dataclass(model_class))
264-
and MSGSPEC_INSTALLED
265266
and preference != "pydantic"
266267
)
267268
)

0 commit comments

Comments
 (0)