From d2a05ddf1dd456f01222558e24bb2bb5b3146f69 Mon Sep 17 00:00:00 2001 From: Stefan VanBuren Date: Sat, 4 Oct 2025 20:47:58 -0400 Subject: [PATCH] Fix truncation logic Pulled out of #135, where I bit off too much. --- src/celpy/celtypes.py | 8 ++++---- tests/test_celtypes.py | 4 ++-- tools/wip.toml | 9 --------- 3 files changed, 6 insertions(+), 15 deletions(-) diff --git a/src/celpy/celtypes.py b/src/celpy/celtypes.py index 3e47271..d3216f1 100644 --- a/src/celpy/celtypes.py +++ b/src/celpy/celtypes.py @@ -190,7 +190,7 @@ import logging import re from functools import reduce, wraps -from math import fsum +from math import fsum, trunc from typing import ( Any, Callable, @@ -545,7 +545,7 @@ class IntType(int): ValueError: overflow >>> IntType(DoubleType(1.9)) - IntType(2) + IntType(1) >>> IntType(DoubleType(-123.456)) IntType(-123) """ @@ -562,7 +562,7 @@ def __new__( # Used by protobuf. return super().__new__(cls, cast(int, source.get(StringType("value")))) elif isinstance(source, (float, DoubleType)): - convert = int64(round) + convert = int64(trunc) elif isinstance(source, TimestampType): convert = int64(lambda src: src.timestamp()) elif isinstance(source, (str, StringType)) and source[:2] in {"0x", "0X"}: @@ -729,7 +729,7 @@ def __new__( if isinstance(source, UintType): return source elif isinstance(source, (float, DoubleType)): - convert = uint64(round) + convert = uint64(trunc) elif isinstance(source, TimestampType): convert = uint64(lambda src: src.timestamp()) elif isinstance(source, (str, StringType)) and source[:2] in {"0x", "0X"}: diff --git a/tests/test_celtypes.py b/tests/test_celtypes.py index c0067f1..4ac9207 100644 --- a/tests/test_celtypes.py +++ b/tests/test_celtypes.py @@ -111,7 +111,7 @@ def test_double_type(): def test_int_type(): i_42 = IntType(42) i_max = IntType(9223372036854775807) - assert IntType(DoubleType(1.9)) == IntType(2) + assert IntType(DoubleType(1.9)) == IntType(1) assert IntType(DoubleType(-123.456)) == IntType(-123) assert IntType(TimestampType("2009-02-13T23:31:30Z")) == 1234567890 assert IntType("0x2a") == 42 @@ -160,7 +160,7 @@ def test_int_type(): def test_uint_type(): u_42 = UintType(42) u_max = UintType(18446744073709551615) - assert UintType(DoubleType(1.9)) == UintType(2) + assert UintType(DoubleType(1.9)) == UintType(1) with pytest.raises(ValueError): assert UintType(DoubleType(-123.456)) == UintType(-123) assert UintType(TimestampType("2009-02-13T23:31:30Z")) == 1234567890 diff --git a/tools/wip.toml b/tools/wip.toml index b2b0709..d51775a 100644 --- a/tools/wip.toml +++ b/tools/wip.toml @@ -239,16 +239,7 @@ string_true_uppercase = "@wip" timestamp = "@wip" [conversions.int] -double_half_neg = "@wip" -double_half_pos = "@wip" double_int_min_range = "@wip" -double_truncate = "@wip" -double_truncate_neg = "@wip" - -[conversions.uint] -double_half = "@wip" -double_truncate = "@wip" - [dynamic.any] literal = "@wip"