Skip to content

Fix pickle and deepcopy of graph types #1133

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

Merged
merged 3 commits into from
Jan 9, 2025

Conversation

robsdedude
Copy link
Member

@robsdedude robsdedude commented Dec 18, 2024

Since the types of Relationships are tied to the Graph object they belong to, fixing pickle support for graph types means that Relationships with the same name will have a different type after deepcopying or pickling and unpickling them or their graph.

Example

Before the fix

import copy
import pickle

import neo4j
import neo4j.graph


g1: neo4j.graph.Graph = some_graph()
g2 = copy.deepcopy(g1)
# g2 = pickle.loads(pickle.dumps(g1))  # errors out

# assuming the graph contained relationships of the type KNOWS
cls_knows1: type[neo4j.graph.Relationship] = g1.relationship_type("KNOWS")
cls_knows2: type[neo4j.graph.Relationship] = g2.relationship_type("KNOWS")
# the types are the same even after the deepcopy
assert cls_knows1 is cls_knows2

# assuming the graph did not contain relationships of the type NEW
cls_new1: type[neo4j.graph.Relationship] = g1.relationship_type("NEW")
cls_new2: type[neo4j.graph.Relationship] = g2.relationship_type("NEW")
# each graph created their own type/class
assert cls_new1 is not cls_new2

After the fix:

g1: neo4j.graph.Graph = some_graph)
g2 = copy.deepcopy(g1)
# or (works now):
# g2 = pickle.loads(pickle.dumps(g1))

# assuming the graph contained relationships of the type KNOWS
cls_knows1: type[neo4j.graph.Relationship] = g1.relationship_type("KNOWS")
cls_knows2: type[neo4j.graph.Relationship] = g2.relationship_type("KNOWS")
# different graphs always track different relationship types/classes
assert cls_knows1 is not cls_knows2

# assuming the graph did not contain relationships of the type NEW
cls_new1: type[neo4j.graph.Relationship] = g1.relationship_type("NEW")
cls_new2: type[neo4j.graph.Relationship] = g2.relationship_type("NEW")
# always different
assert cls_new1 is not cls_new2

Fixes: #1132

Since `Node` and `Relationship`'s equality is based on the equality of the
identity of the graph they're attached to, fixing `pickle` support of graph
types (`Graph`, `Node`, `Relationship`, `Path`) also means that `deepcopy`ed
Graph types are no longer equal to the original ones (as they should've never
been).
@robsdedude robsdedude marked this pull request as ready for review December 19, 2024 13:55
Copy link
Contributor

@StephenCathcart StephenCathcart left a comment

Choose a reason for hiding this comment

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

Looks good!

@robsdedude
Copy link
Member Author

Currently waiting for a discussion with the team how to deal with the change in equality of deep copied graph types.

@robsdedude robsdedude marked this pull request as draft January 8, 2025 10:23
@robsdedude robsdedude marked this pull request as ready for review January 9, 2025 12:37
@robsdedude robsdedude enabled auto-merge (squash) January 9, 2025 12:52
@robsdedude robsdedude merged commit e66ff23 into neo4j:5.0 Jan 9, 2025
20 checks passed
@robsdedude robsdedude deleted the graph-type-pickling branch January 9, 2025 14:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

_pickle.PicklingError: Can't pickle <neo4j.graph.Graph object at 0x7f0fc7b2c4f0>
2 participants