Skip to content

Commit

Permalink
Fix broken serialization for subclasses of MutableMapping
Browse files Browse the repository at this point in the history
  • Loading branch information
Fatal1ty committed Apr 23, 2021
1 parent 90a4ca1 commit 3a9008f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
8 changes: 5 additions & 3 deletions mashumaro/meta/helpers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import dataclasses
import typing
from contextlib import suppress

from .macros import PY_36, PY_37, PY_38, PY_39

Expand Down Expand Up @@ -88,9 +89,10 @@ def is_dataclass_dict_mixin(t):


def is_dataclass_dict_mixin_subclass(t):
for cls in t.__mro__:
if is_dataclass_dict_mixin(cls):
return True
with suppress(AttributeError):
for cls in t.__mro__:
if is_dataclass_dict_mixin(cls):
return True
return False


Expand Down
18 changes: 10 additions & 8 deletions mashumaro/serializer/base/metaprogramming.py
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,10 @@ def _pack_value(
if issubclass(ftype, SerializableType):
return overridden or f"{value_name}._serialize()"

if is_dataclass_dict_mixin_subclass(ftype):
flags = self.get_to_dict_flags(ftype)
return overridden or f"{value_name}.to_dict({flags})"

origin_type = get_type_origin(ftype)
if is_special_typing_primitive(origin_type):
if origin_type is typing.Any:
Expand Down Expand Up @@ -687,9 +691,6 @@ def inner_expr(arg_num=0, v_name="value", v_type=None):
elif issubclass(origin_type, enum.Enum):
specific = f"{value_name}.value"
return f"{value_name} if use_enum else {overridden or specific}"
elif is_dataclass_dict_mixin_subclass(ftype):
flags = self.get_to_dict_flags(ftype)
return overridden or f"{value_name}.to_dict({flags})"
elif overridden:
return overridden

Expand Down Expand Up @@ -727,6 +728,12 @@ def _unpack_field_value(
or f"{type_name(ftype)}._deserialize({value_name})"
)

if is_dataclass_dict_mixin_subclass(ftype):
return overridden or (
f"{type_name(ftype)}.from_dict({value_name}, "
f"use_bytes, use_enum, use_datetime)"
)

origin_type = get_type_origin(ftype)
if is_special_typing_primitive(origin_type):
if origin_type is typing.Any:
Expand Down Expand Up @@ -1027,11 +1034,6 @@ def inner_expr(arg_num=0, v_name="value", v_type=None):
elif issubclass(origin_type, enum.Enum):
specific = f"{type_name(origin_type)}({value_name})"
return f"{value_name} if use_enum else {overridden or specific}"
elif is_dataclass_dict_mixin_subclass(ftype):
return overridden or (
f"{type_name(ftype)}.from_dict({value_name}, "
f"use_bytes, use_enum, use_datetime)"
)
elif overridden:
return overridden

Expand Down

0 comments on commit 3a9008f

Please sign in to comment.