-
-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
A temporary quick fix for dataclass serialization (#500)
This quick fix will be removed when proper dataclass serialization support is added to dill. This is just here to allow for better support, at least for now. dataclasses pickled with this PR will be unpicklable by future versions of dill, but the future versions of dill will be able to be automatically use the newer features in dataclasses.py that were not available in older versions of Python. That forward compatibility features is not present in this PR.
- Loading branch information
1 parent
4462409
commit 1f5814e
Showing
2 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#!/usr/bin/env python | ||
# | ||
# Author: Mike McKerns (mmckerns @caltech and @uqfoundation) | ||
# Author: Anirudh Vegesana (avegesan@cs.stanford.edu) | ||
# Copyright (c) 2022 The Uncertainty Quantification Foundation. | ||
# License: 3-clause BSD. The full license text is available at: | ||
# - https://github.com/uqfoundation/dill/blob/master/LICENSE | ||
""" | ||
test pickling a dataclass | ||
""" | ||
|
||
import dill | ||
import dataclasses | ||
|
||
def test_dataclasses(): | ||
# Issue #500 | ||
@dataclasses.dataclass | ||
class A: | ||
x: int | ||
y: str | ||
|
||
@dataclasses.dataclass | ||
class B: | ||
a: A | ||
|
||
a = A(1, "test") | ||
before = B(a) | ||
save = dill.dumps(before) | ||
after = dill.loads(save) | ||
assert before != after # classes don't match | ||
assert before == B(A(**dataclasses.asdict(after.a))) | ||
assert dataclasses.asdict(before) == dataclasses.asdict(after) | ||
|
||
if __name__ == '__main__': | ||
test_dataclasses() |