Skip to content

Commit 29f99a2

Browse files
committed
dict -> map and support indefinitelist, Datum
1 parent cb35ce5 commit 29f99a2

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

pycardano/plutus.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -447,29 +447,43 @@ def get_tag(constr_id: int) -> Optional[int]:
447447
else:
448448
return None
449449

450+
450451
def id_map(cls, skip_constructor=False):
451452
"""Constructs a unique representation of a PlutusData type definition. Intended for automatic constructor generation."""
452453
if cls == bytes:
453454
return "bytes"
454455
if cls == int:
455456
return "int"
457+
if cls == RawCBOR or cls == RawPlutusData or cls == Datum:
458+
return "any"
459+
if cls == IndefiniteList:
460+
return "list"
456461
if hasattr(cls, "__origin__"):
457462
origin = getattr(cls, "__origin__")
458463
if origin == list:
459464
prefix = "list"
460465
elif origin == dict:
461-
prefix = "dict"
466+
prefix = "map"
462467
elif origin == typing.Union:
463468
prefix = "union"
464469
else:
465-
raise TypeError(f"Unexpected parameterized type for automatic constructor generation: {cls}")
470+
raise TypeError(
471+
f"Unexpected parameterized type for automatic constructor generation: {cls}"
472+
)
466473
return prefix + "<" + ",".join(id_map(a) for a in cls.__args__) + ">"
467474
if issubclass(cls, PlutusData):
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)) + ")"
469-
if cls == RawCBOR or cls == RawPlutusData:
470-
return "any"
475+
return (
476+
"cons["
477+
+ cls.__name__
478+
+ "]("
479+
+ (str(cls.CONSTR_ID) if not skip_constructor else "_")
480+
+ ";"
481+
+ ",".join(f.name + ":" + id_map(f.type) for f in fields(cls))
482+
+ ")"
483+
)
471484
raise TypeError(f"Unexpected type for automatic constructor generation: {cls}")
472485

486+
473487
@dataclass(repr=False)
474488
class PlutusData(ArrayCBORSerializable):
475489
"""

test/pycardano/test_plutus.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
RawPlutusData,
1818
Redeemer,
1919
RedeemerTag,
20-
plutus_script_hash, id_map,
20+
plutus_script_hash,
21+
id_map, Datum,
2122
)
2223
from pycardano.serialization import IndefiniteList, RawCBOR
2324

@@ -397,6 +398,7 @@ class A(PlutusData):
397398
res == res2
398399
), "Same class has different default constructor id in two consecutive runs"
399400

401+
400402
def test_id_map_supports_all():
401403
@dataclass
402404
class A(PlutusData):
@@ -409,6 +411,8 @@ class A(PlutusData):
409411
class C(PlutusData):
410412
x: RawPlutusData
411413
y: RawCBOR
414+
z: Datum
415+
w: IndefiniteList
412416

413417
@dataclass
414418
class B(PlutusData):
@@ -417,6 +421,8 @@ class B(PlutusData):
417421
d: Dict[bytes, C]
418422
e: Union[A, C]
419423

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
424+
s = id_map(B)
425+
assert (
426+
s
427+
== "cons[B](1013743048;a:int,c:cons[A](0;a:int,b:bytes,c:list<int>),d:map<bytes,cons[C](892310804;x:any,y:any,z:any,w:list)>,e:union<cons[A](0;a:int,b:bytes,c:list<int>),cons[C](892310804;x:any,y:any,z:any,w:list)>)"
428+
)

0 commit comments

Comments
 (0)