Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support decoding Decimal types from numeric (int/float) values #463

Merged
merged 3 commits into from
Jul 4, 2023

Conversation

jcrist
Copy link
Owner

@jcrist jcrist commented Jul 4, 2023

This adds support for converting int/float inputs to decimal.Decimal types for all protocols' decode methods, as well msgspec.convert.

The intent of this is to provide more flexible input support for decimal.Decimal types, since not everyone wants to encode them as strings.

For JSON fields there is no loss of precision, the numeric representation is parsed directly into the Decimal object, same as if it was a string.

For all other protocols, there is a possibility of precision loss, when converting from float -> decimal. We don't pass the value directly to decimal.Decimal, but instead convert to a str expressing the shortest roundtrippable value for the IEEE754 decimal. This is effectively equivalent to

if isinstance(input, float):
    return decimal.Decimal(str(input))

In common simple cases this likely does what the user wants. Still, there's a chance of precision loss, see the added documentation for examples of cases where this matters.

For YAML/TOML inputs we could ensure zero precision loss if needed with some extra work, since floats for these protocols are also serialized as str values (same as JSON). I'm punting on this for now until someone asks about it.

For msgpack there's no way around this; since it's a binary protocol, float values are serialized as their binary inputs.

In all cases you really should be serializing decimal.Decimal values as strings, since this is the format that is easiest to avoid precision loss, and is widely and uniformly supported across tools.

jcrist added 3 commits July 3, 2023 20:51
If a field is typed as decimal, JSON numbers will be coerced to a
`decimal.Decimal` value. This is done by parsing the raw JSON bytes, and
won't result in a loss of precision by going through a `double` first.
This finishes up support for converting int/float inputs to
`decimal.Decimal` types in both `msgspec.convert` and
`msgspec.msgpack.decode`.

Note that there may be precision loss when converting from float to
decimal, value dependent. We don't pass the value directly to
`decimal.Decimal`, but instead convert to a str expressing the shortest
roundtrippable value for the IEEE754 decimal. This is effectively
equivalent to

```
if isinstance(input, float):
    return decimal.Decimal(str(input))
```

For YAML/TOML inputs we _could_ ensure zero precision loss if needed
with some extra work, since floats for these protocols are also
serialized as str values. I'm punting on this for now until someone asks
about it.

For msgpack there's no way around this - float values are serialized as
their binary inputs.

In all cases you really should be serializing `decimal.Decimal` values
as strings, since this is the format that is easiest to avoid precision
loss, and is widely and uniformly supported across tools.
@jcrist jcrist merged commit ee9d2ce into main Jul 4, 2023
@jcrist jcrist deleted the decimal-from-numeric branch July 4, 2023 02:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant