pydantic-extra-types icon indicating copy to clipboard operation
pydantic-extra-types copied to clipboard

Support Decimal-backed Coordinates with string-based serialization

Open navignaw opened this issue 11 months ago • 1 comments

Coordinate currently takes a float as input.

Would it be possible to support either a float or Decimal? The main benefit would be using string serialization to preserve precision.

I'm not sure if this applies to pydantic, but in marshmallow docs, there is a warning about using floats:

If a JSON float value is passed to this field for deserialization it will first be cast to its corresponding string value before being deserialized to a decimal.Decimal object. The default str implementation of the built-in Python float type may apply a destructive transformation upon its input data and therefore cannot be relied upon to preserve precision. To avoid this, you can instead pass a JSON string to be deserialized directly.

(If this is not true for pydantic, feel free to close this issue!)

navignaw avatar Jan 11 '25 23:01 navignaw

Yes, it can be unexpected if you use value change:

from pydantic_extra_types.coordinate import Coordinate


coordinate = Coordinate(52.52437, 13.41053)
print(coordinate)
# 52.52437,13.41053
coordinate.latitude -= 23.87056
coordinate.longitude -= -63.81844
print(coordinate)
# got:      28.653809999999996,77.22897
# expected: 28.65381,77.22897

It would be good to support decimal type and operate with them, but also support floating point numbers when validating/serializing. On the other hand, what could this break in current integrations? What do you think?

irtimir avatar May 13 '25 07:05 irtimir