Skip to content

Commit 79979b5

Browse files
authored
Merge pull request #44 from LeakIX/fix-decimal-normalize
Fix Decimal.normalize() causing silent precision loss
2 parents cae5238 + 23b3cc2 commit 79979b5

3 files changed

Lines changed: 14 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ and this project adheres to
2727
### Fixed
2828

2929
- Fix leaked file handles in tests using context managers ([b66a6f5], [#25])
30+
- Fix `Decimal.normalize()` stripping trailing zeros, breaking round-trip
31+
serialization; add regression test ([28c76f1], [0130743], [#28])
3032
- Fix typo in test name: `test_l9events_form_ip4scout` ->
3133
`test_l9events_from_ip4scout` ([0d8736e], [#27])
3234

@@ -152,6 +154,8 @@ and this project adheres to
152154

153155
<!-- Commit links -->
154156

157+
[0130743]: https://github.com/LeakIX/l9format-python/commit/0130743
158+
[28c76f1]: https://github.com/LeakIX/l9format-python/commit/28c76f1
155159
[b66a6f5]: https://github.com/LeakIX/l9format-python/commit/b66a6f5
156160
[cbea4fc]: https://github.com/LeakIX/l9format-python/commit/cbea4fc
157161
[3547e22]: https://github.com/LeakIX/l9format-python/commit/3547e22
@@ -221,6 +225,7 @@ and this project adheres to
221225
[#21]: https://github.com/LeakIX/l9format-python/issues/21
222226
[#22]: https://github.com/LeakIX/l9format-python/issues/22
223227
[#26]: https://github.com/LeakIX/l9format-python/issues/26
228+
[#28]: https://github.com/LeakIX/l9format-python/issues/28
224229
[#27]: https://github.com/LeakIX/l9format-python/issues/27
225230
[#33]: https://github.com/LeakIX/l9format-python/issues/33
226231
[#25]: https://github.com/LeakIX/l9format-python/issues/25

l9format/l9format.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66
def round_decimal(
77
decimal_obj: decimal.Decimal, num_of_places: int = 6
88
) -> decimal.Decimal:
9-
return decimal_obj.quantize(
10-
decimal.Decimal(10) ** -num_of_places
11-
).normalize()
9+
return decimal_obj.quantize(decimal.Decimal(10) ** -num_of_places)
1210

1311

1412
class Decimal(fields.Instance):

tests/test_validation.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,14 @@ def test_geopoint_negative_values(self) -> None:
303303
assert gp.lat == -1.5
304304
assert gp.lon == -2.5
305305

306+
def test_geopoint_round_trip_preserves_value(self) -> None:
307+
"""Regression: normalize() used to strip trailing zeros."""
308+
gp = GeoPoint.from_dict({"lat": "1.000000", "lon": "2.500000"})
309+
serialized = gp.to_dict()
310+
gp2 = GeoPoint.from_dict(serialized)
311+
assert gp2.lat == gp.lat
312+
assert gp2.lon == gp.lon
313+
306314

307315
class TestComplexNestedValidation:
308316
"""Test validation behavior with complex nested structures."""

0 commit comments

Comments
 (0)