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

wrong encoding for decorator objects #467

Open
RRRajput opened this issue Sep 18, 2023 · 1 comment
Open

wrong encoding for decorator objects #467

RRRajput opened this issue Sep 18, 2023 · 1 comment
Labels
bug core has-MRE Has a minimal reproducible example for debugging

Comments

@RRRajput
Copy link

Hi,

jsonpickle is not able to correctly decode decorated functions that return an object.

Here's the minimal reproducible example:

import jsonpickle

class FuncNested:
    def __init__(self, func) -> None:
        self.func = func

    def __call__(self, *args, **kwargs):
        return self.func(*args, **kwargs)

def nester(func):
    return FuncNested(func)


@nester
def bro():
    return "Mate"

# Check that original object is a FuncNested object
assert type(bro) is FuncNested

# Check that the `.func` member variable of the original object is NOT a FuncNested object. 
assert type(bro.func) is not FuncNested

encoded_bro_str = jsonpickle.encode(bro)

print(encoded_bro_str)
>> {"py/object": "__main__.FuncNested", "func": {"py/function": "__main__.bro"}}
decoded_bro = jsonpickle.decode(encoded_bro_str)

# Check that decoded object is also a FuncNested object
assert type(decoded_bro) is FuncNested

# Check that the `.func` member variable of decoded object is NOT a FuncNested object. 
assert type(decoded_bro.func) is not FuncNested # <-- This fails
>> ---------------------------------------------------------------------------
>> AssertionError                            Traceback (most recent call last)
>> Cell In[29], line 26
>>      24 assert type(decoded_bro) is FuncNested
>>      25 # Check that the `.func` member variable is NOT a FuncNested object. 
>> ---> 26 assert type(decoded_bro.func) is not FuncNested # <-- This fails
>> 
>> AssertionError: 
@Theelx
Copy link
Contributor

Theelx commented Sep 18, 2023

That's really interesting, thanks for reporting! I did a bit of further digging, and it seems that before bro gets encoded, bro.func is of type function, and after it is of type FuncNested (the latter being obvious). I think it could be fixed by going back in FuncNested's mro once before it's returned to the user, but I'll need to check later because I'm supposed to be paying attention in class instead of doing unrelated stuff 😅

@Theelx Theelx added bug core has-MRE Has a minimal reproducible example for debugging labels Mar 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug core has-MRE Has a minimal reproducible example for debugging
Projects
None yet
Development

No branches or pull requests

2 participants