Skip to content
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

Multiple usage of same enum constant in dict derived class #357

Open
sch-alex opened this issue Jun 7, 2021 · 2 comments
Open

Multiple usage of same enum constant in dict derived class #357

sch-alex opened this issue Jun 7, 2021 · 2 comments

Comments

@sch-alex
Copy link

sch-alex commented Jun 7, 2021

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 ==============================================```
@Theelx
Copy link
Contributor

Theelx commented Dec 21, 2021

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

@Theelx
Copy link
Contributor

Theelx commented Dec 22, 2021

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: davvid@gmail.com
License: UNKNOWN
Location: /home/theel/.pyenv/versions/3.9.5/lib/python3.9/site-packages
Requires: 
Required-by: 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants