- Python Home
- About
- News
- Documentation
- Downloads
- Community
- Foundation
- Developer's Guide
- Issue Tracker
- Issues
- Summaries
- User
- Administration
- Help
➜
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.
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
@dc.dataclass
class A:
pass
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) * ![]() |
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) * ![]() |
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. |
| History | |||
|---|---|---|---|
| Date | User | Action | Args |
| 2022-04-11 14:59:09 | admin | set | github: 79691 |
| 2018-12-16 15:16:26 | satra | set | status: open -> closed messages: + msg331928 stage: resolved |
| 2018-12-16 15:00:01 | serhiy.storchaka | set | messages: + msg331927 |
| 2018-12-16 14:38:25 | satra | set | messages: + msg331926 |
| 2018-12-16 07:39:41 | serhiy.storchaka | set | nosy:
+ serhiy.storchaka messages: + msg331919 |
| 2018-12-16 04:41:54 | xtreak | set | nosy:
+ eric.smith |
| 2018-12-16 03:22:21 | satra | create |
Powered by Roundup Copyright © 1990-2022, Python Software Foundation
Legal Statements

