improve type coercion; coerce between equal-length uint subclasses
This commit is contained in:
parent
8c6d2b42d8
commit
8bd204827b
|
@ -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
|
# shortcut if it's already the type we are looking for
|
||||||
if v_typ == typ:
|
if v_typ == typ:
|
||||||
return v
|
return v
|
||||||
elif isinstance(v, int) and not isinstance(v, uint): # do not coerce from one uintX to another uintY
|
elif isinstance(v, int):
|
||||||
return typ(v)
|
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)):
|
elif isinstance(v, (list, tuple)):
|
||||||
return typ(*v)
|
return typ(*v)
|
||||||
elif isinstance(v, (bytes, BytesN, Bytes)):
|
elif isinstance(v, (bytes, BytesN, Bytes)):
|
||||||
return typ(v)
|
return typ(v)
|
||||||
elif isinstance(v, GeneratorType):
|
elif isinstance(v, GeneratorType):
|
||||||
return typ(v)
|
return typ(v)
|
||||||
else:
|
|
||||||
# just return as-is, Value-checkers will take care of it not being coerced, if we are not strict.
|
# 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):
|
if strict and not isinstance(v, typ):
|
||||||
raise ValueError("Type coercion of {} to {} failed".format(v, typ))
|
raise ValueError("Type coercion of {} to {} failed".format(v, typ))
|
||||||
return v
|
return v
|
||||||
|
|
||||||
|
|
||||||
class Series(SSZValue):
|
class Series(SSZValue):
|
||||||
|
|
Loading…
Reference in New Issue