diff --git a/src/radical/utils/serialize.py b/src/radical/utils/serialize.py index d87969ba..61c8072f 100644 --- a/src/radical/utils/serialize.py +++ b/src/radical/utils/serialize.py @@ -1,5 +1,4 @@ -import copy import json import msgpack @@ -16,6 +15,7 @@ def __init__(self, ctype, encode, decode): self.encode: callable = encode self.decode: callable = decode + _ctypes = dict() @@ -34,14 +34,12 @@ def register_serializable(cls, encode=None, decode=None): if encode is None: encode = cls if decode is None: decode = cls - global _ctypes _ctypes[cls.__name__] = _CType(cls, encode, decode) # ------------------------------------------------------------------------------ # def _prep_typed_dict(d): - from .typeddict import as_dict return as_dict(d, _annotate=True) @@ -56,13 +54,14 @@ def encode(self, o, *args, **kw): tmp = as_dict(o, _annotate=True) return super().encode(tmp, *args, **kw) - def default(self, obj): - # print('encode: %s' % obj) + def default(self, o): + # print('encode: %s' % o) for cname,methods in _ctypes.items(): - if isinstance(obj, methods.ctype): + if isinstance(o, methods.ctype): return {'_type': cname, - 'as_str': methods.encode(obj)} - return super().default(obj) + 'as_str': methods.encode(o)} + return super().default(o) + # ------------------------------------------------------------------------------ # diff --git a/src/radical/utils/typeddict.py b/src/radical/utils/typeddict.py index 5d606827..87ceb6c7 100644 --- a/src/radical/utils/typeddict.py +++ b/src/radical/utils/typeddict.py @@ -17,7 +17,6 @@ # - optional runtime type checking # -import collections import copy import sys diff --git a/src/radical/utils/zmq/message.py b/src/radical/utils/zmq/message.py index 01661af1..1fa30bdf 100644 --- a/src/radical/utils/zmq/message.py +++ b/src/radical/utils/zmq/message.py @@ -2,7 +2,7 @@ from typing import Dict, Any from ..typeddict import TypedDict -from ..serialize import to_msgpack, from_msgpack, register_serializable +from ..serialize import to_msgpack, from_msgpack # ------------------------------------------------------------------------------ diff --git a/tests/unittests/test_json.py b/tests/unittests/test_json.py index a2c0e564..138cd3a4 100644 --- a/tests/unittests/test_json.py +++ b/tests/unittests/test_json.py @@ -3,7 +3,7 @@ # noqa: E201 -import radical.utils as ru +# import radical.utils as ru # ------------------------------------------------------------------------------ diff --git a/tests/unittests/test_serialization.py b/tests/unittests/test_serialization.py index 77fbd9fd..b685171f 100644 --- a/tests/unittests/test_serialization.py +++ b/tests/unittests/test_serialization.py @@ -55,8 +55,8 @@ class B(ru.TypedDict): new = ru.from_json(ru.to_json(old)) assert old == new - assert type(old) == type(new) - assert type(old['a']) == type(new['a']) + assert isinstance(old, B) and isinstance(new, B) + assert isinstance(old['a'], A) and isinstance(new['a'], A) # ------------------------------------------------------------------------------