Skip to content

Commit 832817f

Browse files
authored
[Bug fix] Return a value directly if its type is 'Any' on deserializing (#195)
1 parent 2b1b98a commit 832817f

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

pycardano/serialization.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22

33
from __future__ import annotations
44

5-
import typing
6-
75
import re
6+
import typing
87
from collections import OrderedDict, UserList, defaultdict
98
from copy import deepcopy
109
from dataclasses import Field, dataclass, fields
@@ -433,7 +432,7 @@ def _restore_typed_primitive(
433432
Returns:
434433
Union[:const:`Primitive`, CBORSerializable]: A CBOR primitive or a CBORSerializable.
435434
"""
436-
if t in PRIMITIVE_TYPES and isinstance(v, t):
435+
if t is Any or (t in PRIMITIVE_TYPES and isinstance(v, t)):
437436
return v
438437
elif isclass(t) and issubclass(t, CBORSerializable):
439438
return t.from_primitive(v)

test/pycardano/test_serialization.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
from typing import Union
2-
31
from dataclasses import dataclass, field
42
from test.pycardano.util import check_two_way_cbor
3+
from typing import Any, Union
54

65
import pytest
76

@@ -162,3 +161,14 @@ def test_indefinite_list():
162161
b.remove(5)
163162
# remove should remove element and return IndefiniteList
164163
assert b == IndefiniteList([4, 6, 7]) and type(b) == IndefiniteList
164+
165+
166+
def test_any_type():
167+
@dataclass
168+
class Test1(MapCBORSerializable):
169+
a: str = ""
170+
b: Any = ""
171+
172+
t = Test1(a="a", b=1)
173+
174+
check_two_way_cbor(t)

0 commit comments

Comments
 (0)