diff --git a/pycardano/plutus.py b/pycardano/plutus.py index a2e058fe..e6a422ef 100644 --- a/pycardano/plutus.py +++ b/pycardano/plutus.py @@ -530,7 +530,7 @@ def _dfs(obj): elif isinstance(obj, list): return [_dfs(item) for item in obj] elif isinstance(obj, IndefiniteList): - return {"list": [_dfs(item) for item in obj.items]} + return {"list": [_dfs(item) for item in obj]} elif isinstance(obj, dict): return {"map": [{"v": _dfs(v), "k": _dfs(k)} for k, v in obj.items()]} elif isinstance(obj, PlutusData): diff --git a/pycardano/serialization.py b/pycardano/serialization.py index 5e40a150..bf9d58a4 100644 --- a/pycardano/serialization.py +++ b/pycardano/serialization.py @@ -3,7 +3,7 @@ from __future__ import annotations import re -from collections import OrderedDict, defaultdict +from collections import OrderedDict, defaultdict, UserList from copy import deepcopy from dataclasses import Field, dataclass, fields from datetime import datetime @@ -36,15 +36,9 @@ ] -class IndefiniteList: - def __init__(self, items): - self.items = items - - def __eq__(self, other): - if isinstance(other, IndefiniteList): - return self.items == other.items - else: - return False +class IndefiniteList(UserList): + def __init__(self, list: [Primitive]): # type: ignore + super().__init__(list) @dataclass @@ -146,7 +140,7 @@ def default_encoder( # handling here to explicitly write header (b'\x9f'), each body item, and footer (b'\xff') to # the output bytestring. encoder.write(b"\x9f") - for item in value.items: + for item in value: encoder.encode(item) encoder.write(b"\xff") elif isinstance(value, RawCBOR): @@ -240,7 +234,7 @@ def _dfs(value): elif isinstance(value, list): return [_helper(k) for k in value] elif isinstance(value, IndefiniteList): - return IndefiniteList([_helper(k) for k in value.items]) + return IndefiniteList([_helper(k) for k in value]) elif isinstance(value, CBORTag): return CBORTag(value.tag, _helper(value.value)) else: diff --git a/test/pycardano/test_serialization.py b/test/pycardano/test_serialization.py index 0f236980..5d63c39a 100644 --- a/test/pycardano/test_serialization.py +++ b/test/pycardano/test_serialization.py @@ -10,6 +10,7 @@ DictCBORSerializable, MapCBORSerializable, limit_primitive_type, + IndefiniteList, ) @@ -133,3 +134,31 @@ def test_dict_cbor_serializable(): # Make sure the cbor of a and b are exactly the same even when their items are inserted in different orders. assert a.to_cbor() == b.to_cbor() + + +def test_indefinite_list(): + + a = IndefiniteList([4, 5]) + + a.append(6) + # append should add element and return IndefiniteList + assert a == IndefiniteList([4, 5, 6]) and type(a) == IndefiniteList + + b = a + IndefiniteList([7, 8]) + # addition of two IndefiniteLists should return IndefiniteList + assert type(b) == IndefiniteList + + a.extend([7, 8]) + # extend should add elements and return IndefiniteList + assert a == IndefiniteList([4, 5, 6, 7, 8]) and type(a) == IndefiniteList + + # testing eq operator + assert a == b + + b.pop() + # pop should remove last element and return IndefiniteList + assert b == IndefiniteList([4, 5, 6, 7]) and type(b) == IndefiniteList + + b.remove(5) + # remove should remove element and return IndefiniteList + assert b == IndefiniteList([4, 6, 7]) and type(b) == IndefiniteList