dataclasses-json
dataclasses-json copied to clipboard
Add support for enhanced builtin instantiation
What's in this change?
- Adding improved instantiation logic for
str
,int
,float
in dataclass decoder. - Fixing bugs in test code
Why do we need improved instantiation logic for these builtins?
There are a few reasons why instantiating using the intended type when they are one of the three is beneficial.
-
Support for custom
str
classes is covered. Previously this was not supported asstr
is treated special in the decode logic. This is further documented here: https://github.com/lidatong/dataclasses-json/issues/348 -
Improved support for conversion from input X ->
int
|float
. Previously, values that represented numeric values (either stringified numeric values orDecimal
objects) were not converted properly into the intended field type. So a dataclass field specified as anint
would not decode"42"
ordecimal.Decimal("42")
as42
. This differs from marshmallow decode behavior which properly converts to the integer value.
Testing
Added tests to demonstrate new usage
Note that this will fail tests until this PR is merged: https://github.com/lidatong/dataclasses-json/pull/374