You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by we...@apache.org on 2017/10/31 18:11:23 UTC
[arrow] branch master updated: ARROW-1753: [Python] Provide for
matching subclasses with register_type in serialization context
This is an automated email from the ASF dual-hosted git repository.
wesm pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow.git
The following commit(s) were added to refs/heads/master by this push:
new 9dc4c58 ARROW-1753: [Python] Provide for matching subclasses with register_type in serialization context
9dc4c58 is described below
commit 9dc4c58d57159edf24bcbe86b6220a6a3bcb09ef
Author: dhirschf <da...@stanwell.com>
AuthorDate: Tue Oct 31 14:11:19 2017 -0400
ARROW-1753: [Python] Provide for matching subclasses with register_type in serialization context
https://issues.apache.org/jira/browse/ARROW-1753
Author: dhirschf <da...@stanwell.com>
Author: Philipp Moritz <pc...@gmail.com>
Closes #1272 from dhirschfeld/ARROW-1753 and squashes the following commits:
bb7f041a [Philipp Moritz] fix subclass serialization tests
cbc5e09c [dhirschf] Added a test that register_type will work for subclasses of the registered type
46b60f44 [dhirschf] Allow register_type to match subclasses in serialization context
---
python/pyarrow/serialization.pxi | 17 ++++++--
python/pyarrow/serialization.py | 2 +
python/pyarrow/tests/test_serialization.py | 66 ++++++++++++++++++++++++++++++
python/requirements.txt | 2 +-
4 files changed, 82 insertions(+), 5 deletions(-)
diff --git a/python/pyarrow/serialization.pxi b/python/pyarrow/serialization.pxi
index 4e9ab8e..6b72277 100644
--- a/python/pyarrow/serialization.pxi
+++ b/python/pyarrow/serialization.pxi
@@ -88,17 +88,26 @@ cdef class SerializationContext:
self.custom_deserializers[type_id] = custom_deserializer
def _serialize_callback(self, obj):
- if type(obj) not in self.type_to_type_id:
+ found = False
+ for type_ in type(obj).__mro__:
+ if type_ in self.type_to_type_id:
+ found = True
+ break
+
+ if not found:
raise SerializationCallbackError(
"pyarrow does not know how to "
- "serialize objects of type {}.".format(type(obj)), obj)
- type_id = self.type_to_type_id[type(obj)]
+ "serialize objects of type {}.".format(type(obj)), obj
+ )
+
+ # use the closest match to type(obj)
+ type_id = self.type_to_type_id[type_]
if type_id in self.types_to_pickle:
serialized_obj = {"data": pickle.dumps(obj), "pickle": True}
elif type_id in self.custom_serializers:
serialized_obj = {"data": self.custom_serializers[type_id](obj)}
else:
- if is_named_tuple(type(obj)):
+ if is_named_tuple(type_):
serialized_obj = {}
serialized_obj["_pa_getnewargs_"] = obj.__getnewargs__()
elif hasattr(obj, "__dict__"):
diff --git a/python/pyarrow/serialization.py b/python/pyarrow/serialization.py
index 9dc8ee6..2b47513 100644
--- a/python/pyarrow/serialization.py
+++ b/python/pyarrow/serialization.py
@@ -69,6 +69,8 @@ def register_default_serialization_handlers(serialization_context):
type(lambda: 0), "function",
pickle=True)
+ serialization_context.register_type(type, "type", pickle=True)
+
# ----------------------------------------------------------------------
# Set up serialization for numpy with dtype object (primitive types are
# handled efficiently with Arrow's Tensor facilities, see
diff --git a/python/pyarrow/tests/test_serialization.py b/python/pyarrow/tests/test_serialization.py
index 7878a09..b0c5bc4 100644
--- a/python/pyarrow/tests/test_serialization.py
+++ b/python/pyarrow/tests/test_serialization.py
@@ -416,3 +416,69 @@ def test_serialization_callback_error():
with pytest.raises(pa.DeserializationCallbackError) as err:
serialized_object.deserialize(deserialization_context)
assert err.value.type_id == 20*b"\x00"
+
+
+def test_fallback_to_subclasses():
+
+ class SubFoo(Foo):
+ def __init__(self):
+ Foo.__init__(self)
+
+ # should be able to serialize/deserialize an instance
+ # if a base class has been registered
+ serialization_context = pa.SerializationContext()
+ serialization_context.register_type(Foo, "Foo")
+
+ subfoo = SubFoo()
+ # should fallbact to Foo serializer
+ serialized_object = pa.serialize(subfoo, serialization_context)
+
+ reconstructed_object = serialized_object.deserialize(
+ serialization_context
+ )
+ assert type(reconstructed_object) == Foo
+
+
+class Serializable(object):
+ pass
+
+
+def serialize_serializable(obj):
+ return {"type": type(obj), "data": obj.__dict__}
+
+
+def deserialize_serializable(obj):
+ val = obj["type"].__new__(obj["type"])
+ val.__dict__.update(obj["data"])
+ return val
+
+
+class SerializableClass(Serializable):
+ def __init__(self):
+ self.value = 3
+
+
+def test_serialize_subclasses():
+
+ # This test shows how subclasses can be handled in an idiomatic way
+ # by having only a serializer for the base class
+
+ # This technique should however be used with care, since pickling
+ # type(obj) with couldpickle will include the full class definition
+ # in the serialized representation.
+ # This means the class definition is part of every instance of the
+ # object, which in general is not desirable; registering all subclasses
+ # with register_type will result in faster and more memory
+ # efficient serialization.
+
+ serialization_context.register_type(
+ Serializable, "Serializable",
+ custom_serializer=serialize_serializable,
+ custom_deserializer=deserialize_serializable)
+
+ a = SerializableClass()
+ serialized = pa.serialize(a)
+
+ deserialized = serialized.deserialize()
+ assert type(deserialized).__name__ == SerializableClass.__name__
+ assert deserialized.value == 3
diff --git a/python/requirements.txt b/python/requirements.txt
index d2e28a7..8d0c33a 100644
--- a/python/requirements.txt
+++ b/python/requirements.txt
@@ -1,4 +1,4 @@
pytest
-cloudpickle
+cloudpickle>=0.4.0
numpy>=1.10.0
six
--
To stop receiving notification emails like this one, please contact
['"commits@arrow.apache.org" <co...@arrow.apache.org>'].