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

Pickle support #147

Merged
merged 6 commits into from
Apr 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions ppb_vector/vector2.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ def __new__(cls, *args, **kwargs):

return self

def __reduce__(self):
return type(self).__new__, (type(self), self.x, self.y)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will that prevent subclasses (esp. ones that add attributes) to be pickled correctly?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The improved test answers this positively, in the case of subclasses without extra attributes.

I guess it would make sense to punt the with-attributes case to #144.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pickle docs mention higher-level protocols, but they're not actually implemented by the core stuff? not super clear.


#: Return a new :py:class:`Vector2` replacing specified fields with new values.
update = dataclasses.replace

Expand Down
12 changes: 12 additions & 0 deletions tests/test_vector2_ctor.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import pickle

import pytest # type: ignore
from hypothesis import given

Expand Down Expand Up @@ -38,3 +40,13 @@ def test_ctor_noncopy_subclass():
def test_ctor_noncopy_superclass():
v = Vector2(1, 2)
assert V(v) is not v


@pytest.mark.parametrize("cls", [Vector2, V])
@given(v=vectors())
def test_ctor_pickle(cls, v: Vector2):
"""Round-trip Vector2 and subclasses through `pickle.{dumps,loads}`."""
w = pickle.loads(pickle.dumps(cls(v)))

assert v == w
assert isinstance(w, cls)