dataclasses-json: [BUG] Type coercion since 0.5.9
Description
I’ll let you guys explain that this is intended and why, but starting with 0.5.9 there is type coercion going on when using the from_dict() method.
A non-zero int becomes True on a bool field, for instance. An int becomes its stringified version on a string field.
This feels to me like a breaking change, but I don’t see this anywhere in the release notes. Nor in the version number in any case.
Was this intended? If so, why?
Code snippet that reproduces the issue
from dataclasses import dataclass
from dataclasses_json import DataClassJsonMixin
@dataclass
class MyDataclass(DataClassJsonMixin):
this_must_be_bool: bool
this_must_be_string: str
d = {"this_must_be_bool": 1234, "this_must_be_string": 1234}
c = MyDataclass.from_dict(d)
assert isinstance(c.this_must_be_bool, bool)
assert isinstance(c.this_must_be_string, str)
Describe the results you expected
So this snippet raises with 0.5.8. Anything after and including 0.5.9 does not raise.
Python version you are using
3.11.4
Environment description
Tested with dataclasses-json 0.5.9, 0.5.12 and 0.5.14
About this issue
- Original URL
- State: closed
- Created a year ago
- Comments: 17
Opened an issue for v1 - when we get there, would be nice if you have time to participate in the discussion 😃 Thanks a lot for bringing this up!
Alright, then this is what we’ll do. I think we’ll have some commits to release by end of this week, so expect 0.6.0 around Sunday 😃
Bumping to a new version and then documenting seems to me the best thing to do in this case. In the release notes for 0.6.0, just specify that there’s a fix for a regression introduced in 0.5.9, and then explain what is the intended behavior from this version onward.
@george-zubrienko in v1 all these coercions should be a feature flag. It may still be the default behavior btw.