From 8bd204827bd5e2d94091ede1e9ff973b011a7678 Mon Sep 17 00:00:00 2001 From: protolambda Date: Thu, 20 Jun 2019 21:08:34 +0200 Subject: [PATCH] improve type coercion; coerce between equal-length uint subclasses --- .../pyspec/eth2spec/utils/ssz/ssz_typing.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/test_libs/pyspec/eth2spec/utils/ssz/ssz_typing.py b/test_libs/pyspec/eth2spec/utils/ssz/ssz_typing.py index e5f9f66a8..55acde44b 100644 --- a/test_libs/pyspec/eth2spec/utils/ssz/ssz_typing.py +++ b/test_libs/pyspec/eth2spec/utils/ssz/ssz_typing.py @@ -94,19 +94,24 @@ def coerce_type_maybe(v, typ: SSZType, strict: bool = False): # shortcut if it's already the type we are looking for if v_typ == typ: return v - elif isinstance(v, int) and not isinstance(v, uint): # do not coerce from one uintX to another uintY - return typ(v) + elif isinstance(v, int): + if isinstance(v, uint): # do not coerce from one uintX to another uintY + if issubclass(typ, uint) and v.type().byte_len == typ.byte_len: + return typ(v) + # revert to default behavior below if-else. (ValueError/bare) + else: + return typ(v) elif isinstance(v, (list, tuple)): return typ(*v) elif isinstance(v, (bytes, BytesN, Bytes)): return typ(v) elif isinstance(v, GeneratorType): return typ(v) - else: - # just return as-is, Value-checkers will take care of it not being coerced, if we are not strict. - if strict and not isinstance(v, typ): - raise ValueError("Type coercion of {} to {} failed".format(v, typ)) - return v + + # just return as-is, Value-checkers will take care of it not being coerced, if we are not strict. + if strict and not isinstance(v, typ): + raise ValueError("Type coercion of {} to {} failed".format(v, typ)) + return v class Series(SSZValue):