coerce container types, since classes do not match between phases

This commit is contained in:
protolambda 2020-01-05 23:38:06 +01:00
parent 2e6090fcd8
commit 3995bd633c
No known key found for this signature in database
GPG Key ID: EC89FDBB2B4C7623

View File

@ -119,6 +119,8 @@ def coerce_type_maybe(v, typ: SSZType, strict: bool = False):
return typ(v)
elif isinstance(v, GeneratorType):
return typ(v)
elif issubclass(typ, Container) and not isinstance(v, typ):
return typ(**{field_name: getattr(v, field_name) for field_name in typ.get_field_names()})
# 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):
@ -192,7 +194,7 @@ class Container(Series, metaclass=SSZType):
return dict(cls.__annotations__)
@classmethod
def get_field_names(cls) -> Iterable[SSZType]:
def get_field_names(cls) -> Iterable[str]:
if not hasattr(cls, '__annotations__'): # no container fields
return ()
return list(cls.__annotations__.keys())