uqfoundation / dill

serialize all of Python

Home Page:http://dill.rtfd.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Metadata of wrapper functions not preserved

dfremont opened this issue · comments

dill seems to not preserve the metadata of functions wrapped with functools.wraps. With dill 0.3.6 on Python 3.11.1, the following code:

import functools

import dill

def a(x):
    return 42

@functools.wraps(a)
def b(x):
    return a(x)

data = dill.dumps(b)
c = dill.loads(data)

print(f'{a=}, {b=}, {c=}')

prints something along the lines of

a=<function a at 0x108164a40>, b=<function a at 0x1081963e0>, c=<function b at 0x108ca2fc0>

where the unpickled function should have name a like the original does.

I thought the issue might be that the __qualname__ attribute of the function wasn't getting saved, but setting that attribute manually rather than using functools.wraps actually works; the code

def d(x):
    return 42

d.__qualname__ = 'foo'

data = dill.dumps(d)
e = dill.loads(data)

print(f'{d=}, {e=}')

prints something like

d=<function foo at 0x10bd58860>, e=<function foo at 0x10c892fc0>

where now the name of the unpickled function is correct. So I don't know what's going on.

Hi @mmckerns, I'm still seeing this issue with dill 0.3.7 -- any idea what the cause might be? I haven't been able to figure out why the second snippet above works while the first one doesn't.