You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tinkerpop.apache.org by GitBox <gi...@apache.org> on 2018/10/29 18:18:08 UTC

[GitHub] spmallette closed pull request #964: TINKERPOP-2071: gremlin-python: g:Set graphson deserializer should return a python set

spmallette closed pull request #964: TINKERPOP-2071: gremlin-python: g:Set graphson deserializer should return a python set
URL: https://github.com/apache/tinkerpop/pull/964
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/CHANGELOG.asciidoc b/CHANGELOG.asciidoc
index 9a47f3c9c9..184c1d9454 100644
--- a/CHANGELOG.asciidoc
+++ b/CHANGELOG.asciidoc
@@ -23,6 +23,7 @@ image::https://raw.githubusercontent.com/apache/tinkerpop/master/docs/static/ima
 [[release-3-3-5]]
 === TinkerPop 3.3.5 (Release Date: NOT OFFICIALLY RELEASED YET)
 
+* gremlin-python: the graphson deserializer for g:Set should return a python set
 
 [[release-3-3-4]]
 === TinkerPop 3.3.4 (Release Date: October 15, 2018)
diff --git a/docs/src/reference/gremlin-variants.asciidoc b/docs/src/reference/gremlin-variants.asciidoc
index 074c436f41..4bc2f36646 100644
--- a/docs/src/reference/gremlin-variants.asciidoc
+++ b/docs/src/reference/gremlin-variants.asciidoc
@@ -307,9 +307,10 @@ g.V().out().map(lambda: "x: len(x.get().value('name'))").sum().toList()
 
 === Limitations
 
-* Traversals that return a `Set` will be coerced to a `List` in Python so that traversals return consistent results
-within a collection across different languages. In the case of Python, number equality is different from JVM languages
-which produces different `Set` results when those types are in use. If a `Set` is needed then convert `List` results
+* Traversals that return a `Set` *might* be coerced to a `List` in Python. In the case of Python, number equality 
+is different from JVM languages which produces different `Set` results when those types are in use. When this case
+is detected during deserialization, the `Set` is coerced to a `List` so that traversals return consistent
+results within a collection across different languages. If a `Set` is needed then convert `List` results
 to `Set` manually.
 
 [[gremlin-DotNet]]
diff --git a/gremlin-python/src/main/jython/gremlin_python/structure/io/graphsonV3d0.py b/gremlin-python/src/main/jython/gremlin_python/structure/io/graphsonV3d0.py
index a04142180d..65e01afe96 100644
--- a/gremlin-python/src/main/jython/gremlin_python/structure/io/graphsonV3d0.py
+++ b/gremlin-python/src/main/jython/gremlin_python/structure/io/graphsonV3d0.py
@@ -22,6 +22,7 @@
 import uuid
 import math
 from collections import OrderedDict
+import logging
 
 import six
 from aenum import Enum
@@ -31,6 +32,8 @@
 from gremlin_python.process.traversal import Binding, Bytecode, P, Traversal, Traverser, TraversalStrategy, T
 from gremlin_python.structure.graph import Edge, Property, Vertex, VertexProperty, Path
 
+log = logging.getLogger(__name__)
+
 # When we fall back to a superclass's serializer, we iterate over this map.
 # We want that iteration order to be consistent, so we use an OrderedDict,
 # not a dict.
@@ -429,11 +432,20 @@ def dictify(cls, s, writer):
 
     @classmethod
     def objectify(cls, s, reader):
-        # coerce to list here because Java might return numerics of different types which python won't recognize
-        # see comments of TINKERPOP-1844 for more details
-        new_set = []
-        for obj in s:
-            new_set.append(reader.toObject(obj))
+        """
+        By default, returns a python set
+
+        In case Java returns numeric values of different types which
+        python don't recognize, coerce and return a list.
+        See comments of TINKERPOP-1844 for more details
+        """
+        new_list = [reader.toObject(obj) for obj in s]
+        new_set = set(new_list)
+        if len(new_list) != len(new_set):
+            log.warning("Coercing g:Set to list due to java numeric values. "
+                        "See TINKERPOP-1844 for more details.")
+            return new_list
+
         return new_set
 
 
diff --git a/gremlin-python/src/main/jython/tests/structure/io/test_graphsonV3d0.py b/gremlin-python/src/main/jython/tests/structure/io/test_graphsonV3d0.py
index 4aa4eed001..f1268bd3df 100644
--- a/gremlin-python/src/main/jython/tests/structure/io/test_graphsonV3d0.py
+++ b/gremlin-python/src/main/jython/tests/structure/io/test_graphsonV3d0.py
@@ -51,11 +51,20 @@ def test_collections(self):
         assert x[1] == 2
         assert x[2] == "3"
         ##
+
         x = self.graphson_reader.readObject(
             json.dumps({"@type": "g:Set", "@value": [{"@type": "g:Int32", "@value": 1},
                                                      {"@type": "g:Int32", "@value": 2},
-                                                     {"@type": "g:Float", "@value": 2.0},
                                                      "3"]}))
+        # return a set as normal
+        assert isinstance(x, set)
+        assert x == set([1, 2, "3"])
+
+        x = self.graphson_reader.readObject(
+            json.dumps({"@type": "g:Set", "@value": [{"@type": "g:Int32", "@value": 1},
+                                                    {"@type": "g:Int32", "@value": 2},
+                                                    {"@type": "g:Float", "@value": 2.0},
+                                                    "3"]}))
         # coerce to list here because Java might return numerics of different types which python won't recognize
         # see comments of TINKERPOP-1844 for more details
         assert isinstance(x, list)
diff --git a/gremlin-test/features/map/Select.feature b/gremlin-test/features/map/Select.feature
index d4fdd04295..468346453a 100644
--- a/gremlin-test/features/map/Select.feature
+++ b/gremlin-test/features/map/Select.feature
@@ -724,6 +724,6 @@ Feature: Step - select()
     When iterated next
     Then the result should be ordered
       | result |
-      | l[a,b] |
-      | l[c]   |
+      | s[a,b] |
+      | s[c]   |
     And the graph should return 6 for count of "g.V().as(\"a\", \"b\").out().as(\"c\").path().select(Column.keys)"


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services