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

fails to load a @dataclass instance when serialized=False, fails to dump when serialized=True #71

Open
jvickroy opened this issue May 6, 2019 · 4 comments
Labels

Comments

@jvickroy
Copy link

jvickroy commented May 6, 2019

klepto 0.1.6 (running in Python 3.7.3) raises a ReferenceError exception in _dill.py @ line 1148 when attempting to dump a dir_archive with serialized=True (the default).

The following sample script illustrates the behavior (see SERIALIZED).

from   dataclasses import dataclass
@dataclass (frozen=True)
class Foo:
    name : str

foo = Foo (name='klepto')
key = foo.name
d   = dict (key=foo)
import klepto
SERIALIZED = False ## WORKS!
SERIALIZED = True  ## raises ReferenceError: {} does not reference a class __dict__ @ ..Python37\lib\site-packages\dill\_dill.py", line 1148, in save_dictproxy
a = klepto.archives.dir_archive (dict=d, serialized=SERIALIZED)
a.dump()
@mmckerns
Copy link
Member

mmckerns commented May 6, 2019

Thanks for reporting. In terms of the case serialized=True, that's an issue for dill, not klepto -- and it should be fixed by dill. I'd close this issue, and suggest you report to dill. However, you might note that serialized=False actually doesn't work -- at least for me. See below:

>>> from   dataclasses import dataclass
>>> @dataclass (frozen=True)
... class Foo:
...     name : str
... 
>>> foo = Foo (name='klepto')
>>> key = foo.name
>>> d   = dict (key=foo)
>>> import klepto
>>> SERIALIZED = False
>>> a = klepto.archives.dir_archive (dict=d, serialized=SERIALIZED)
>>> a.dump()
>>> 
>>> a.clear()
>>> a.load()
Traceback (most recent call last):
  File "/Users/mmckerns/lib/python3.7/site-packages/klepto/_archives.py", line 632, in _lookup
    exec(string, globals()) #FIXME: unsafe, potential name conflict
  File "<string>", line 1, in <module>
  File "/Users/mmckerns/src/python-tmp/memo/K_key/__init__.py", line 3, in <module>
    memo = Foo(name='klepto')
TypeError: Foo() takes no arguments

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/mmckerns/lib/python3.7/site-packages/klepto/_archives.py", line 152, in load
    self.update(self.archive.__asdict__())
  File "/Users/mmckerns/lib/python3.7/site-packages/klepto/_archives.py", line 406, in __asdict__
    return dict((key,self.__getitem__(key)) for key in keys)
  File "/Users/mmckerns/lib/python3.7/site-packages/klepto/_archives.py", line 406, in <genexpr>
    return dict((key,self.__getitem__(key)) for key in keys)
  File "/Users/mmckerns/lib/python3.7/site-packages/klepto/_archives.py", line 436, in __getitem__
    return self._lookup(key)
  File "/Users/mmckerns/lib/python3.7/site-packages/klepto/_archives.py", line 636, in _lookup
    raise KeyError(key)
KeyError: 'key'
>>> 

Since most objects serialize well, serialized=True is the default, and the dataclass object is used less frequently than some other objects... it seems this case is a klepto issue. I'll change the title to reflect the issue.

@mmckerns mmckerns changed the title dill doesn't know how to serialize @dataclass instances fails to load a @dataclass instance when serialized=False, fails to dump when serialized=True May 6, 2019
@mmckerns mmckerns added the bug label May 6, 2019
@jvickroy
Copy link
Author

jvickroy commented May 6, 2019 via email

@mmckerns
Copy link
Member

@jvickroy: the bug appears to be fixed in dill in master.

@jvickroy
Copy link
Author

Thanks for alerting me to this. /jim

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

No branches or pull requests

2 participants