This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: pickling derived dataclasses
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.7
Status: closed Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: eric.smith, satra, serhiy.storchaka
Priority: normal Keywords:

Created on 2018-12-16 03:22 by satra, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (5)
msg331914 - (view) Author: Satrajit S Ghosh (satra) Date: 2018-12-16 03:22
I'm not sure if this is intended behavior or an error. I'm creating dataclasses dynamically and trying to pickle those classes or objects containing instances of those classes. This was resulting in an error, so I trimmed it down to this example.

import pickle as pk
import dataclasses as dc

class A:
pk.dumps(A)  # --> this is fine

B = dc.make_dataclass('B', [], bases=(A,))
pk.dumps(B)  # --> results in an error

# PicklingError: Can't pickle <class 'types.B'>: attribute lookup B on types failed

is this expected behavior? and if so, is there a way to create a dynamic dataclass that pickles?
msg331919 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-12-16 07:39
You need to set the __module__ attribute.

B.__module__ = __name__
msg331926 - (view) Author: Satrajit S Ghosh (satra) Date: 2018-12-16 14:38
thank you + serhiy.storchaka

while that works in an interactive namespace, it fails in the following setting, which is closer to my dynamic use case.

class A:
    def __init__(self, fields=None):
        self.B = dc.make_dataclass('B', fields or [])
        self.B.__module__ = __name__
        # ...
        self.C = self.B()

now pickling A() fails.
msg331927 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-12-16 15:00
Dataclasses are pickled by name, as well as other classes. Pickling classes which can not be accessed by name is not supported.
msg331928 - (view) Author: Satrajit S Ghosh (satra) Date: 2018-12-16 15:16
Thank you.
Date User Action Args
2022-04-11 14:59:09adminsetgithub: 79691
2018-12-16 15:16:26satrasetstatus: open -> closed

messages: + msg331928
stage: resolved
2018-12-16 15:00:01serhiy.storchakasetmessages: + msg331927
2018-12-16 14:38:25satrasetmessages: + msg331926
2018-12-16 07:39:41serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg331919
2018-12-16 04:41:54xtreaksetnosy: + eric.smith
2018-12-16 03:22:21satracreate