Skip to content

Commit b1cda02

Browse files
authored
Fix/parse raw plutus data (#220)
* Add test that fails for Datum type * Fix deserialization to make type annotation datum work * Fix deserialization test, add iterable case
1 parent 3c76964 commit b1cda02

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

pycardano/plutus.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -693,7 +693,7 @@ def from_primitive(cls: Type[RawPlutusData], value: CBORTag) -> RawPlutusData:
693693
return cls(value)
694694

695695

696-
Datum = Union[PlutusData, dict, IndefiniteList, int, bytes, RawCBOR, RawPlutusData]
696+
Datum = Union[PlutusData, dict, int, bytes, IndefiniteList, RawCBOR, RawPlutusData]
697697
"""Plutus Datum type. A Union type that contains all valid datum types."""
698698

699699

pycardano/serialization.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,10 @@ def _restore_typed_primitive(
496496
raise DeserializeException(f"Expected type list but got {type(v)}")
497497
return IndefiniteList([_restore_typed_primitive(t, w) for w in v])
498498
elif isclass(t) and issubclass(t, IndefiniteList):
499-
return IndefiniteList(v)
499+
try:
500+
return IndefiniteList(v)
501+
except TypeError:
502+
raise DeserializeException(f"Can not initialize IndefiniteList from {v}")
500503
elif hasattr(t, "__origin__") and (t.__origin__ is dict):
501504
t_args = t.__args__
502505
if len(t_args) != 2:

test/pycardano/test_serialization.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
import cbor2
12
from dataclasses import dataclass, field
3+
4+
from pycardano import Datum, RawPlutusData
25
from test.pycardano.util import check_two_way_cbor
36
from typing import Any, Dict, List, Optional, Set, Tuple, Union
47

@@ -174,6 +177,22 @@ class Test1(MapCBORSerializable):
174177
check_two_way_cbor(t)
175178

176179

180+
def test_datum_type():
181+
@dataclass
182+
class Test1(MapCBORSerializable):
183+
b: Datum
184+
185+
# make sure that no "not iterable" error is thrown
186+
t = Test1(b=RawPlutusData(cbor2.CBORTag(125, [])))
187+
188+
check_two_way_cbor(t)
189+
190+
# Make sure that iterable objects are not deserialized to the wrong object
191+
t = Test1(b=b"hello!")
192+
193+
check_two_way_cbor(t)
194+
195+
177196
def test_wrong_primitive_type():
178197
@dataclass
179198
class Test1(MapCBORSerializable):

0 commit comments

Comments
 (0)