Skip to content

Commit 7edfdfa

Browse files
Add __.V() wrapping for Vertex objects in to() and from() methods
- Wrap Vertex objects with __.V(vertex.id) in to() and from() steps - Use isinstance() check to only wrap actual Vertex objects - Revert __.V() wrapping for basic gremlin python examples - Update integration tests to match and test new behavior
1 parent 586d4e9 commit 7edfdfa

File tree

4 files changed

+13
-5
lines changed

4 files changed

+13
-5
lines changed

CHANGELOG.asciidoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ image::https://raw.githubusercontent.com/apache/tinkerpop/master/docs/static/ima
8282
* Added `idleConnectionTimeout` setting for Gremlin Driver and automatic closing of idle connections
8383
* Enabled TCP Keep-Alive in GremlinServer.
8484
* Updated Python GLV examples to use HTTP and to run as part of the integration tests.
85+
* Fixed `gremlin-python` `to()` and `from_()` methods to wrap Vertex objects with `__.V()` for proper edge creation.
8586
8687
== TinkerPop 3.8.0 (Grix Greven)
8788

gremlin-python/src/main/python/examples/basic_gremlin.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ def main():
4040

4141
# be sure to use a terminating step like next() or iterate() so that the traversal "executes"
4242
# iterate() does not return any data and is used to just generate side-effects (i.e. write data to the database)
43-
g.V(v1).add_e('knows').to(__.V(v2)).property('weight', 0.75).iterate()
44-
g.V(v1).add_e('knows').to(__.V(v3)).property('weight', 0.75).iterate()
43+
g.V(v1).add_e('knows').to(v2).property('weight', 0.75).iterate()
44+
g.V(v1).add_e('knows').to(v3).property('weight', 0.75).iterate()
4545

4646
# retrieve the data from the "marko" vertex
4747
marko = g.V().has(VERTEX_LABEL, 'name', 'marko').values('name').next()

gremlin-python/src/main/python/gremlin_python/process/graph_traversal.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
from ..driver.remote_connection import RemoteStrategy
2929
from .. import statics
3030
from ..statics import long
31+
from ..structure.graph import Vertex
3132

3233
log = logging.getLogger("gremlinpython")
3334

@@ -506,7 +507,10 @@ def format_(self, *args):
506507
return self
507508

508509
def from_(self, *args):
509-
self.gremlin_lang.add_step("from", *args)
510+
if len(args) == 1 and isinstance(args[0], Vertex):
511+
self.gremlin_lang.add_step("from", __.V(args[0].id))
512+
else:
513+
self.gremlin_lang.add_step("from", *args)
510514
return self
511515

512516
def group(self, *args):
@@ -943,7 +947,10 @@ def times(self, *args):
943947
return self
944948

945949
def to(self, *args):
946-
self.gremlin_lang.add_step("to", *args)
950+
if len(args) == 1 and isinstance(args[0], Vertex):
951+
self.gremlin_lang.add_step("to", __.V(args[0].id))
952+
else:
953+
self.gremlin_lang.add_step("to", *args)
947954
return self
948955

949956
def toE(self, *args):

gremlin-python/src/main/python/tests/process/test_traversal.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ def test_should_extract_id_from_vertex(self):
370370

371371
# Test edge creation with from/to vertices
372372
from_to = g.add_e("Edge").from_(Vertex(1)).to(Vertex(2))
373-
assert "g.addE('Edge').from(1).to(2)" == from_to.gremlin_lang.get_gremlin()
373+
assert "g.addE('Edge').from(__.V(1)).to(__.V(2))" == from_to.gremlin_lang.get_gremlin()
374374

375375
# Test mergeE() with Vertex in dictionary
376376
merge_map = {

0 commit comments

Comments
 (0)