Skip to content

Commit

Permalink
v9.5.3, fix transform for integer 0, add callable transformer, fix en…
Browse files Browse the repository at this point in the history
…coder for Decimal
  • Loading branch information
voidZXL committed Aug 16, 2024
1 parent 4a30833 commit 7631162
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 5 deletions.
8 changes: 6 additions & 2 deletions tests/test_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ def trans_my(trans, d, t):
([10, 11], 10, False, False),
((-1.3 + 0j), -1, False, False),
(en_z, 0, True, True), # this enum instance is an instance of int
('', 0, False, True),
(None, 0, False, True),
],
SubInt: [
(1, SubInt(1), True, True),
Expand Down Expand Up @@ -530,13 +532,15 @@ class en(Enum):
'time': time(12, 13, 14, 1234),
'dur': timedelta(days=1, seconds=10, microseconds=123),
'dc': Decimal('10.23'),
'di': Decimal('-11'),
'd0': Decimal('0'),
'en': en(2),
'a': (1, 2),
's': {'s'}
}
res = JSONSerializer().dumps(data)
assert res == b'{"dt":"2000-01-01T12:13:14.001234","date":"2000-01-01",' \
b'"time":"12:13:14.001","dur":"P1DT00H00M10.000123S","dc":10.23,"en":2,"a":[1,2],"s":["s"]}'
assert res == (b'{"dt":"2000-01-01T12:13:14.001234","date":"2000-01-01","time":"12:13:14.001",'
b'"dur":"P1DT00H00M10.000123S","dc":10.23,"di":-11,"d0":0,"en":2,"a":[1,2],"s":["s"]}')

# def test_vendor(self):
# from utype import register_transformer
Expand Down
2 changes: 1 addition & 1 deletion utype/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
register_transformer = TypeTransformer.registry.register


VERSION = (0, 5, 2, None)
VERSION = (0, 5, 3, None)


def _get_version():
Expand Down
1 change: 1 addition & 0 deletions utype/parser/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ def resolve_forward_refs(self, local_vars=None, ignore_errors: bool = True):
return False
clear_refs = []
resolved = False
# todo: add resolve hooks so that application code can execute lazy-load type process logic
for name in list(self.forward_refs):
ref, constraints = self.forward_refs[name]
try:
Expand Down
6 changes: 5 additions & 1 deletion utype/utils/encode.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,14 @@ def from_uuid(data: uuid.UUID):

@register_encoder(decimal.Decimal)
def from_decimal(data: decimal.Decimal):
if data.is_normal():
if data.is_finite():
if js_unsafe(data):
return str(data)
if not data.as_tuple().exponent:
# integer
return int(data)
return float(data)
# infinity / NaN
return str(data)


Expand Down
11 changes: 10 additions & 1 deletion utype/utils/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import io
import json
import re
import collections
from collections import deque
from collections.abc import Iterable, Iterator, Mapping, Sequence
from datetime import date, datetime, time, timedelta, timezone
Expand Down Expand Up @@ -401,6 +402,8 @@ def to_integer(self, data, t: Type[int] = int) -> int:
return 0
if data.lower() in self.TRUE_VALUES:
return 1
elif isinstance(data, t):
return data

try:
data = Decimal(data)
Expand All @@ -410,7 +413,7 @@ def to_integer(self, data, t: Type[int] = int) -> int:
# FOR number > 1e+16, int(float()) will not get accurate result, use decimal instead

if self.no_data_loss:
if not data.is_normal():
if not data.is_finite():
raise TypeError
if data.as_tuple().exponent:
raise TypeError
Expand Down Expand Up @@ -646,6 +649,12 @@ def to_type(self, data, t=type) -> type:
raise TypeError('data is not a type')
return data

@registry.register(collections.abc.Callable, allow_subclasses=False)
def to_callable(self, data, t) -> Callable:
if not callable(data):
raise TypeError('data is not callable')
return data

def handle_unresolved(self, data, t):
if isinstance(t, type) and isinstance(data, t):
# we just loosely match the isinstance for unresolved types
Expand Down

0 comments on commit 7631162

Please sign in to comment.