Skip to content

Commit

Permalink
Fix unexpected dialect while using DataClassMessagePackMixin
Browse files Browse the repository at this point in the history
  • Loading branch information
Fatal1ty committed Jun 8, 2022
1 parent a254654 commit bc43c09
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
4 changes: 3 additions & 1 deletion mashumaro/core/meta/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -573,9 +573,11 @@ def get_from_dict_default_flag_values(self, cls=None) -> str:
return pluggable_flags_str

def is_code_generation_option_enabled(self, option: str, cls=None) -> bool:
if cls is None:
cls = self.cls
if option == ADD_DIALECT_SUPPORT:
# TODO: make inheritance for code_generation_options
for ancestor in self.cls.__mro__[-1:0:-1]:
for ancestor in cls.__mro__[-1:0:-1]:
if (
type_name(ancestor)
== "mashumaro.mixins.msgpack.DataClassMessagePackMixin"
Expand Down
27 changes: 27 additions & 0 deletions tests/test_dialect.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
from mashumaro import DataClassDictMixin
from mashumaro.config import ADD_DIALECT_SUPPORT, BaseConfig
from mashumaro.dialect import Dialect
from mashumaro.dialects.msgpack import MessagePackDialect
from mashumaro.exceptions import BadDialect
from mashumaro.mixins.msgpack import DataClassMessagePackMixin
from mashumaro.mixins.msgpack import default_encoder as msgpack_encoder
from mashumaro.types import SerializationStrategy

from .conftest import fake_add_from_dict
Expand Down Expand Up @@ -190,6 +193,13 @@ class Config(BaseConfig):
code_generation_options = [ADD_DIALECT_SUPPORT]


@dataclass
class MessagePackDataClass(DataClassMessagePackMixin):
b: bytes
bb: bytearray
dep: DataClassWithoutDialects


def test_default_dialect():
dt = date.today()
ordinal = dt.toordinal()
Expand Down Expand Up @@ -875,3 +885,20 @@ def test_dialect_with_inheritance():
)
assert entity1.to_dict(dialect=FormattedDialect) == {"dt1": formatted}
assert entity2.to_dict(dialect=FormattedDialect) == {"dt2": formatted}


def test_msgpack_dialect_class_with_dependency_without_dialect():
dt = date(2022, 6, 8)
obj = MessagePackDataClass(
b=b"123",
bb=bytearray([4, 5, 6]),
dep=DataClassWithoutDialects(dt, 123),
)
d = {
"b": b"123",
"bb": bytearray([4, 5, 6]),
"dep": {"dt": "2022-06-08", "i": 123},
}
encoded = msgpack_encoder(d)
assert obj.to_msgpack() == encoded
assert MessagePackDataClass.from_msgpack(encoded) == obj

0 comments on commit bc43c09

Please sign in to comment.