diff --git a/CHANGELOG.md b/CHANGELOG.md index 4560660..9a3d0ff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,6 +27,8 @@ and this project adheres to ### Fixed - Fix leaked file handles in tests using context managers ([b66a6f5], [#25]) +- Fix `Decimal.normalize()` stripping trailing zeros, breaking round-trip + serialization; add regression test ([28c76f1], [0130743], [#28]) - Fix typo in test name: `test_l9events_form_ip4scout` -> `test_l9events_from_ip4scout` ([0d8736e], [#27]) @@ -152,6 +154,8 @@ and this project adheres to +[0130743]: https://github.com/LeakIX/l9format-python/commit/0130743 +[28c76f1]: https://github.com/LeakIX/l9format-python/commit/28c76f1 [b66a6f5]: https://github.com/LeakIX/l9format-python/commit/b66a6f5 [cbea4fc]: https://github.com/LeakIX/l9format-python/commit/cbea4fc [3547e22]: https://github.com/LeakIX/l9format-python/commit/3547e22 @@ -221,6 +225,7 @@ and this project adheres to [#21]: https://github.com/LeakIX/l9format-python/issues/21 [#22]: https://github.com/LeakIX/l9format-python/issues/22 [#26]: https://github.com/LeakIX/l9format-python/issues/26 +[#28]: https://github.com/LeakIX/l9format-python/issues/28 [#27]: https://github.com/LeakIX/l9format-python/issues/27 [#33]: https://github.com/LeakIX/l9format-python/issues/33 [#25]: https://github.com/LeakIX/l9format-python/issues/25 diff --git a/l9format/l9format.py b/l9format/l9format.py index 74b46a9..4bf6102 100644 --- a/l9format/l9format.py +++ b/l9format/l9format.py @@ -6,9 +6,7 @@ def round_decimal( decimal_obj: decimal.Decimal, num_of_places: int = 6 ) -> decimal.Decimal: - return decimal_obj.quantize( - decimal.Decimal(10) ** -num_of_places - ).normalize() + return decimal_obj.quantize(decimal.Decimal(10) ** -num_of_places) class Decimal(fields.Instance): diff --git a/tests/test_validation.py b/tests/test_validation.py index 69bbb80..e506869 100644 --- a/tests/test_validation.py +++ b/tests/test_validation.py @@ -303,6 +303,14 @@ def test_geopoint_negative_values(self) -> None: assert gp.lat == -1.5 assert gp.lon == -2.5 + def test_geopoint_round_trip_preserves_value(self) -> None: + """Regression: normalize() used to strip trailing zeros.""" + gp = GeoPoint.from_dict({"lat": "1.000000", "lon": "2.500000"}) + serialized = gp.to_dict() + gp2 = GeoPoint.from_dict(serialized) + assert gp2.lat == gp.lat + assert gp2.lon == gp.lon + class TestComplexNestedValidation: """Test validation behavior with complex nested structures."""