Fatal1ty / mashumaro

Fast and well tested serialization library
Apache License 2.0
774 stars 45 forks source link

`__post_deserialize__` broken on `str`, `bool` #120

Closed cj-darius-lapunas closed 1 year ago

cj-darius-lapunas commented 1 year ago

Description

__post_deserialize__ hook causes deserialization errors with str or bool fields. Tested using DataClassDictMixin and Python versions 3.7-3.10.

What I Did

Narrowed down the simplest repro I could.

str example

@dataclass
class Data(DataClassDictMixin):
    foo: str

    @classmethod
    def __post_deserialize__(cls, obj):
        return obj

print(Data.from_dict({'foo': 'bar'}))

result:

Traceback (most recent call last):
  File "....", line 16, in <module>
    print(Data.from_dict({'foo': 'bar'}))
  File "<string>", line 13, in from_dict
TypeError: __init__() missing 1 required positional argument: 'foo'

bool example

@dataclass
class Data(DataClassDictMixin):
    foo: bool

    @classmethod
    def __post_deserialize__(cls, obj):
        return obj

print(Data.from_dict({'foo': True}))

result:

Traceback (most recent call last):
  File "....", line 16, in <module>
    print(Data.from_dict({'foo': True}))
  File "<string>", line 13, in from_dict
TypeError: __init__() missing 1 required positional argument: 'foo'

int example

@dataclass
class Data(DataClassDictMixin):
    foo: int

    @classmethod
    def __post_deserialize__(cls, obj):
        return obj

print(Data.from_dict({'foo': 5}))

result:

Data(foo=5)

no hook

@dataclass
class Data(DataClassDictMixin):
    foo: bool

print(Data.from_dict({'foo': True}))

result:

Data(foo=True)
Fatal1ty commented 1 year ago

@cj-darius-lapunas

Thank you for finding the bug so quickly. I fixed it and will release 3.8.1 today.

Fatal1ty commented 1 year ago

Fixed in 3.8.1

cj-darius-lapunas commented 1 year ago

Looks good on my end, thanks for the quick fix.