neo4j-contrib / py2neo

EOL! Py2neo is a comprehensive Neo4j driver library and toolkit for Python.
https://py2neo.org
Apache License 2.0
20 stars 8 forks source link

Union of nodes and relationships does not work as intented in Python 3.9 #871

Closed adrienrougny closed 3 years ago

adrienrougny commented 3 years ago

Python 3.9 introduced merge (|) and update (|=) operators to the dict class. When merging nodes and relationships in Python 3.9, it seems that it is the __or__/__ior__ method of the dict class that is called rather the __or__ method of the Subgraph class, because of inheritance and MRO.

Jim89 commented 3 years ago

I think this is causing an issue for my current project.

We previously had code that looked (something) like this:

import py2neo
g = py2neo.Graph("my_graph_address")
result = g.run("match (n :a_label) return n limit 10")
raw_sg = result.to_subgraph()
nodes = [n for n in raw_sg.nodes]
rels = [r for r in raw_sg.relationships]

further_processing(nodes)
further_processing(rels)

I've just upgraded to 3.9, and it looks like this is no longer working, I only get the last record back in raw_sg, meaning the full set of nodes/relationships cannot be returned. I think we can probably code around it or fall back to an earlier version of Python (thanks for filing the issue @Adrienrougny, I suspect I wouldn't have figured this out otherwise!).

I agree that the __or__() of dict does look like it's getting called, as if I run something like this:

raw_result = g.run("[a query]")
sg_list = []
for result in raw_result:
    new_sg = result.to_subgraph()
    sg_list.append(new_sg)

sg_list[0].__or__(sg_list[1])

I get a dict back, and not a Subgraph as I would expect.

technige commented 3 years ago

This has already been fixed in https://github.com/technige/py2neo/commit/881cd3fcd05538d4dff1409d72135353446ec117, although it has not yet been released.

Jim89 commented 3 years ago

Oh brill, thanks! I've fallen back to 3.8 for now so it's not hugely pressing, but do you know when you plan to do the next release? In either case, thanks very much for the response and the package!@

technige commented 3 years ago

Hopefully not long before I do another one, though I don't have a date in mind quite yet.

technige commented 3 years ago

I actually decided to release tonight! So your fix should be live in 2021.0.0.