jsonpickle
jsonpickle copied to clipboard
Multiple usage of same enum constant in dict derived class
I have a special use case which has worked until version 1.5.2. I'm using a class derived from dict and store enum constants as variables. If some of the enum constants are the same the decode process does not work correctly.
Attached example:
from enum import IntEnum
import jsonpickle as json
class MyEnum(IntEnum):
ONE = (1,)
TWO = (2,)
class MyDict(dict):
def __init__(self, _enum1, _enum2):
self._enum1 = _enum1
self._enum2 = _enum2
def __repr__(self):
return repr(self.__dict__)
def _test(_enum1, _enum2):
obj1 = MyDict(_enum1, _enum2)
obj2 = json.decode(json.dumps(obj1))
print(f"obj1: {obj1}")
print(f"obj2: {obj2}")
return (obj1._enum1 == obj2._enum1) and (obj1._enum2 == obj2._enum2)
def test_1():
assert _test(MyEnum.ONE, MyEnum.ONE)
def test_2():
assert _test(MyEnum.ONE, MyEnum.TWO)
======================================================= FAILURES =======================================================
________________________________________________________ test_1 ________________________________________________________
def test_1():
> assert _test(MyEnum.ONE, MyEnum.ONE)
E assert False
E + where False = _test(<MyEnum.ONE: 1>, <MyEnum.ONE: 1>)
E + where <MyEnum.ONE: 1> = MyEnum.ONE
E + and <MyEnum.ONE: 1> = MyEnum.ONE
test_jsonpickle.py:29: AssertionError
------------------------------------------------- Captured stdout call -------------------------------------------------
obj1: {'_enum1': <MyEnum.ONE: 1>, '_enum2': <MyEnum.ONE: 1>}
obj2: {'_enum1': <MyEnum.ONE: 1>, '_enum2': {...}}
=============================================== short test summary info ================================================
FAILED test_jsonpickle.py::test_1 - assert False
============================================= 1 failed, 1 passed in 0.08s ==============================================```
I apologize for the delay in getting back to you, I forgot to check jsonpickle's repo for a few months. Looking at the code, I have no idea what the issue is. Have you narrowed down the issue to a specific commit (with git bisect or something) between 1.5.1 and 1.5.2, or should I do that? Once I can identify the issue, I can probably get a fix in for 2.0.1
Oh, I just tried reproducing this, and it worked fine on jsonpickle 1.5.2:
theel@theel:~/Coding$ py.test sch_alex_jsonpickle_1_5_2.py
============================= test session starts ==============================
platform linux -- Python 3.9.5, pytest-6.2.4, py-1.10.0, pluggy-0.13.1
benchmark: 3.4.1 (defaults: timer=time.perf_counter disable_gc=False min_rounds=5 min_time=0.000005 max_time=1.0 calibration_precision=10 warmup=False warmup_iterations=100000)
rootdir: /home/theel/Coding
plugins: benchmark-3.4.1, hypothesis-6.21.6, black-0.3.12, asyncio-0.15.1, black-multipy-1.0.1, cov-2.12.1, flake8-1.0.7, xdist-2.3.0, forked-1.3.0, anyio-3.3.0
collected 2 items
sch_alex_jsonpickle_1_5_2.py .. [100%]
============================== 2 passed in 0.06s ===============================
(you'll notice it has my benchmarking plugins enabled, that's just a default setting. it's still testing the code you wrote though)
jsonpickle version:
theel@theel:~/Coding$ pip show jsonpickle
Name: jsonpickle
Version: 1.5.2
Summary: Python library for serializing any arbitrary object graph into JSON
Home-page: https://github.com/jsonpickle/jsonpickle
Author: David Aguilar
Author-email: [email protected]
License: UNKNOWN
Location: /home/theel/.pyenv/versions/3.9.5/lib/python3.9/site-packages
Requires:
Required-by: