Skip to content

Commit cb35ce5

Browse files
committed
Add test to check that id_map supports complex types
1 parent 928a4e4 commit cb35ce5

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

pycardano/plutus.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -463,9 +463,9 @@ def id_map(cls, skip_constructor=False):
463463
prefix = "union"
464464
else:
465465
raise TypeError(f"Unexpected parameterized type for automatic constructor generation: {cls}")
466-
return prefix + "(" + ",".join(id_map(a) for a in cls.__args__) + ")"
466+
return prefix + "<" + ",".join(id_map(a) for a in cls.__args__) + ">"
467467
if issubclass(cls, PlutusData):
468-
return "constructor:" + cls.__name__ + "(" + (str(cls.CONSTR_ID) if not skip_constructor else "_") + ";" + ",".join(f.name + ":" + id_map(f.type) for f in fields(cls)) + ")"
468+
return "cons[" + cls.__name__ + "](" + (str(cls.CONSTR_ID) if not skip_constructor else "_") + ";" + ",".join(f.name + ":" + id_map(f.type) for f in fields(cls)) + ")"
469469
if cls == RawCBOR or cls == RawPlutusData:
470470
return "any"
471471
raise TypeError(f"Unexpected type for automatic constructor generation: {cls}")

test/pycardano/test_plutus.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
RawPlutusData,
1818
Redeemer,
1919
RedeemerTag,
20-
plutus_script_hash,
20+
plutus_script_hash, id_map,
2121
)
22-
from pycardano.serialization import IndefiniteList
22+
from pycardano.serialization import IndefiniteList, RawCBOR
2323

2424

2525
@dataclass
@@ -396,3 +396,27 @@ class A(PlutusData):
396396
assert (
397397
res == res2
398398
), "Same class has different default constructor id in two consecutive runs"
399+
400+
def test_id_map_supports_all():
401+
@dataclass
402+
class A(PlutusData):
403+
CONSTR_ID = 0
404+
a: int
405+
b: bytes
406+
c: List[int]
407+
408+
@dataclass
409+
class C(PlutusData):
410+
x: RawPlutusData
411+
y: RawCBOR
412+
413+
@dataclass
414+
class B(PlutusData):
415+
a: int
416+
c: A
417+
d: Dict[bytes, C]
418+
e: Union[A, C]
419+
420+
s = id_map(B, skip_constructor=True)
421+
assert s == "cons[B](_;a:int,c:cons[A](0;a:int,b:bytes,c:list<int>),d:dict<bytes,cons[C](3081122523;x:any,y:any)>,e:union<cons[A](0;a:int,b:bytes,c:list<int>),cons[C](3081122523;x:any,y:any)>)"
422+
assert B.CONSTR_ID == 2561434002

0 commit comments

Comments
 (0)