2

Is there a possibility to pickle/dill an_instance developed as follows?

import dill
import pandas as pd
import numpy as np
from datetime import datetime, timezone
from dataclasses import make_dataclass, field

fields = [('float_val', float, field(default=np.nan)),
          ('df', pd.DataFrame, field(default_factory=pd.DataFrame)),
          ('int_val', int, field(default_factory=int)),
          ('time_val', datetime, field(default=datetime.now(timezone.utc)))]

Aclass = make_dataclass('Aclass', fields)

an_instance=Aclass()

dill.dump(an_instance, open('test.pkl', 'wb'))

The dump gives PicklingError: Can't pickle <class 'types.Aclass'>: it's not found as types.Aclass

reservoirinvest
  • 1,463
  • 2
  • 16
  • 32
  • What does the `dill` documentation say are the requirements of an object to be pickled? Have you searched with the error message? – wwii Dec 20 '20 at 16:30
  • Did you see this one? [How can I pickle a dynamically created nested class in python?](https://stackoverflow.com/questions/1947904/how-can-i-pickle-a-dynamically-created-nested-class-in-python) It is a similar issue. – Tom Myddeltyn Dec 20 '20 at 16:37

2 Answers2

3

Thx @Tom Myddeltyn for Issue35510 bug tracker. dill.dump and dill.load works by setting the name using Aclass.__module__ = __name__.

Full code is as follows:

fields = [('float_val', float, field(default=np.nan)),
          ('df', pd.DataFrame, field(default_factory=pd.DataFrame)),
          ('int_val', int, field(default_factory=int)),
          ('time_val', datetime, field(default=datetime.now(timezone.utc)))]

Aclass = make_dataclass('Aclass', fields)
Aclass.__module__ = __name__ # provide name for pickling the class

an_instance=Aclass()

dill.dump(an_instance, open('test.pkl', 'wb'))
dill.load(open('test.pkl', 'rb'))
reservoirinvest
  • 1,463
  • 2
  • 16
  • 32
1

I found that this is unsupported in pickle which probably means dill as well. Python bug tracker Issue35510 :pickling derived dataclasses

Dataclasses are pickled by name, as well as other classes. Pickling classes which can not be accessed by name is not supported. - Serhiy Storchaka (2018-12-16)

Also see here for more details with dill. github dill issue tracker

Tom Myddeltyn
  • 1,307
  • 1
  • 13
  • 27
  • I believe your answer gets invalidated by the other answer that provides a solution. – Rub Feb 04 '22 at 17:31