dataclasses-json icon indicating copy to clipboard operation
dataclasses-json copied to clipboard

Add support for enhanced builtin instantiation

Open rpmcginty opened this issue 1 year ago • 0 comments

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.

  1. Support for custom str classes is covered. Previously this was not supported as str is treated special in the decode logic. This is further documented here: https://github.com/lidatong/dataclasses-json/issues/348
  2. Improved support for conversion from input X -> int|float. Previously, values that represented numeric values (either stringified numeric values or Decimal objects) were not converted properly into the intended field type. So a dataclass field specified as an int would not decode "42" or decimal.Decimal("42") as 42. 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

rpmcginty avatar Sep 27 '22 00:09 rpmcginty