Skip to content

Commit d2a05dd

Browse files
Fix truncation logic
Pulled out of cloud-custodian#135, where I bit off too much.
1 parent 443a79f commit d2a05dd

File tree

3 files changed

+6
-15
lines changed

3 files changed

+6
-15
lines changed

src/celpy/celtypes.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@
190190
import logging
191191
import re
192192
from functools import reduce, wraps
193-
from math import fsum
193+
from math import fsum, trunc
194194
from typing import (
195195
Any,
196196
Callable,
@@ -545,7 +545,7 @@ class IntType(int):
545545
ValueError: overflow
546546
547547
>>> IntType(DoubleType(1.9))
548-
IntType(2)
548+
IntType(1)
549549
>>> IntType(DoubleType(-123.456))
550550
IntType(-123)
551551
"""
@@ -562,7 +562,7 @@ def __new__(
562562
# Used by protobuf.
563563
return super().__new__(cls, cast(int, source.get(StringType("value"))))
564564
elif isinstance(source, (float, DoubleType)):
565-
convert = int64(round)
565+
convert = int64(trunc)
566566
elif isinstance(source, TimestampType):
567567
convert = int64(lambda src: src.timestamp())
568568
elif isinstance(source, (str, StringType)) and source[:2] in {"0x", "0X"}:
@@ -729,7 +729,7 @@ def __new__(
729729
if isinstance(source, UintType):
730730
return source
731731
elif isinstance(source, (float, DoubleType)):
732-
convert = uint64(round)
732+
convert = uint64(trunc)
733733
elif isinstance(source, TimestampType):
734734
convert = uint64(lambda src: src.timestamp())
735735
elif isinstance(source, (str, StringType)) and source[:2] in {"0x", "0X"}:

tests/test_celtypes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def test_double_type():
111111
def test_int_type():
112112
i_42 = IntType(42)
113113
i_max = IntType(9223372036854775807)
114-
assert IntType(DoubleType(1.9)) == IntType(2)
114+
assert IntType(DoubleType(1.9)) == IntType(1)
115115
assert IntType(DoubleType(-123.456)) == IntType(-123)
116116
assert IntType(TimestampType("2009-02-13T23:31:30Z")) == 1234567890
117117
assert IntType("0x2a") == 42
@@ -160,7 +160,7 @@ def test_int_type():
160160
def test_uint_type():
161161
u_42 = UintType(42)
162162
u_max = UintType(18446744073709551615)
163-
assert UintType(DoubleType(1.9)) == UintType(2)
163+
assert UintType(DoubleType(1.9)) == UintType(1)
164164
with pytest.raises(ValueError):
165165
assert UintType(DoubleType(-123.456)) == UintType(-123)
166166
assert UintType(TimestampType("2009-02-13T23:31:30Z")) == 1234567890

tools/wip.toml

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -239,16 +239,7 @@ string_true_uppercase = "@wip"
239239
timestamp = "@wip"
240240

241241
[conversions.int]
242-
double_half_neg = "@wip"
243-
double_half_pos = "@wip"
244242
double_int_min_range = "@wip"
245-
double_truncate = "@wip"
246-
double_truncate_neg = "@wip"
247-
248-
[conversions.uint]
249-
double_half = "@wip"
250-
double_truncate = "@wip"
251-
252243
[dynamic.any]
253244
literal = "@wip"
254245

0 commit comments

Comments
 (0)