jcrist · GitHub

@jcrist

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.

Read the original on github.com ↗