Skip to content

Commit 4c59455

Browse files
committed
Add unknown fields to ArraySerializable if more values are provided
1 parent d7a5eec commit 4c59455

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

pycardano/serialization.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,10 @@ def from_primitive(cls: ArrayBase, values: List[Primitive]) -> ArrayBase:
489489
f.type = type_hints[f.name]
490490
v = _restore_dataclass_field(f, v)
491491
restored_vals.append(v)
492-
return cls(*restored_vals)
492+
obj = cls(*restored_vals)
493+
for i in range(len(all_fields), len(values)):
494+
setattr(obj, f"unknown_field{i - len(all_fields)}", values[i])
495+
return obj
493496

494497
def __repr__(self):
495498
return super().__repr__()
@@ -728,7 +731,9 @@ def copy(self) -> DictBase:
728731

729732

730733
@typechecked
731-
def list_hook(cls: Type[CBORBase]) -> Callable[[List[Primitive]], List[CBORBase]]:
734+
def list_hook(
735+
cls: Type[CBORSerializable],
736+
) -> Callable[[List[Primitive]], List[CBORBase]]:
732737
"""A factory that generates a Callable which turns a list of Primitive to a list of CBORSerializables.
733738
734739
Args:

test/pycardano/test_serialization.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@ class Test2(ArrayCBORSerializable):
2424
check_two_way_cbor(t)
2525

2626

27+
def test_array_cbor_serializable_unknown_fields():
28+
@dataclass
29+
class Test1(ArrayCBORSerializable):
30+
a: str
31+
b: str
32+
33+
t = Test1.from_primitive(["a", "b", "c"])
34+
assert hasattr(t, "unknown_field0") and t.unknown_field0 == "c"
35+
36+
2737
def test_array_cbor_serializable_optional_field():
2838
@dataclass
2939
class Test1(ArrayCBORSerializable):

0 commit comments

Comments
 (0)