Skip to content

Commit

Permalink
Bugfix using msgspec without attrs
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
pgjones committed May 20, 2024
1 parent 1e9d3e8 commit 00a596b
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/quart_schema/conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ class PydanticValidationError(Exception): # type: ignore

try:
from attrs import fields as attrs_fields, has as is_attrs
except ImportError:

def is_attrs(object_: Any) -> bool: # type: ignore
return False


try:
from msgspec import convert, Struct, to_builtins, ValidationError as MsgSpecValidationError
from msgspec.json import schema_components
except ImportError:
Expand All @@ -52,9 +59,6 @@ class PydanticValidationError(Exception): # type: ignore
class Struct: # type: ignore
pass

def is_attrs(object_: Any) -> bool: # type: ignore
return False

def convert(object_: Any, type_: Any) -> Any: # type: ignore
raise RuntimeError("Cannot convert, msgspec not installed")

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


def _use_pydantic(model_class: Type, preference: Optional[str]) -> bool:
return (
return PYDANTIC_INSTALLED and (
is_pydantic_dataclass(model_class)
or (isclass(model_class) and issubclass(model_class, BaseModel))
or (
(_is_list_or_dict(model_class) or is_dataclass(model_class))
and PYDANTIC_INSTALLED
and preference != "msgspec"
(_is_list_or_dict(model_class) or is_dataclass(model_class)) and preference != "msgspec"
)
)


def _use_msgspec(model_class: Type, preference: Optional[str]) -> bool:
return (
return MSGSPEC_INSTALLED and (
(isclass(model_class) and issubclass(model_class, Struct))
or is_attrs(model_class)
or (
(_is_list_or_dict(model_class) or is_dataclass(model_class))
and MSGSPEC_INSTALLED
and preference != "pydantic"
)
)

0 comments on commit 00a596b

Please sign in to comment.