Skip to content

[Bug fix] Return a value directly if its type is 'Any' on deserializing #195

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions pycardano/serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

from __future__ import annotations

import typing

import re
import typing
from collections import OrderedDict, UserList, defaultdict
from copy import deepcopy
from dataclasses import Field, dataclass, fields
Expand Down Expand Up @@ -433,7 +432,7 @@ def _restore_typed_primitive(
Returns:
Union[:const:`Primitive`, CBORSerializable]: A CBOR primitive or a CBORSerializable.
"""
if t in PRIMITIVE_TYPES and isinstance(v, t):
if t is Any or (t in PRIMITIVE_TYPES and isinstance(v, t)):
return v
elif isclass(t) and issubclass(t, CBORSerializable):
return t.from_primitive(v)
Expand Down
14 changes: 12 additions & 2 deletions test/pycardano/test_serialization.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from typing import Union

from dataclasses import dataclass, field
from test.pycardano.util import check_two_way_cbor
from typing import Any, Union

import pytest

Expand Down Expand Up @@ -162,3 +161,14 @@ def test_indefinite_list():
b.remove(5)
# remove should remove element and return IndefiniteList
assert b == IndefiniteList([4, 6, 7]) and type(b) == IndefiniteList


def test_any_type():
@dataclass
class Test1(MapCBORSerializable):
a: str = ""
b: Any = ""

t = Test1(a="a", b=1)

check_two_way_cbor(t)