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

Fix pickling dataclasses #245

Merged
merged 10 commits into from
Feb 6, 2019
Merged
7 changes: 7 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
0.8.0
=====

- Add support for pickling interactively defined dataclasses.
([issue #245](https://github.com/cloudpipe/cloudpickle/pull/245))


0.7.0
=====

Expand Down
6 changes: 6 additions & 0 deletions cloudpickle/cloudpickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -904,6 +904,12 @@ def save_root_logger(self, obj):

dispatch[logging.RootLogger] = save_root_logger

if hasattr(types, "MappingProxyType"):
ogrisel marked this conversation as resolved.
Show resolved Hide resolved
def _save_mappingproxy(self, obj):
self.save_reduce(types.MappingProxyType, (dict(obj),), obj=obj)

dispatch[types.MappingProxyType] = _save_mappingproxy

"""Special functions for Add-on libraries"""
def inject_addons(self):
"""Plug in system. Register additional pickling functions if modules already loaded"""
Expand Down
15 changes: 15 additions & 0 deletions tests/cloudpickle_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1344,6 +1344,21 @@ def __init__(self):
with pytest.raises(AttributeError):
obj.non_registered_attribute = 1

@unittest.skipIf(not hasattr(types, "MappingProxyType"),
"Old versions of Python do not have this type.")
def test_mappingproxy(self):
mp = types.MappingProxyType({"some_key": "some value"})
assert mp == pickle_depickle(mp, protocol=self.protocol)

def test_dataclass(self):
dataclasses = pytest.importorskip("dataclasses")

DataClass = dataclasses.make_dataclass('DataClass', [('x', int)])
data = DataClass(x=42)

pickle_depickle(DataClass, protocol=self.protocol)
assert data.x == pickle_depickle(data, protocol=self.protocol).x == 42


class Protocol2CloudPickleTest(CloudPickleTest):

Expand Down