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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
See also https://github.com/neo4j/neo4j-python-driver/wiki for a full changelog.

## NEXT RELEASE
- No breaking or major changes.
- Since the types of `Relationship`s are tied to the `Graph` object they belong to, fixing `pickle` support for graph types means that `Relationship`s with the same name will have a different type after `deepcopy`ing or pickling and unpickling them or their graph.
For more details, see https://github.com/neo4j/neo4j-python-driver/pull/1133


## Version 5.27
Expand Down
23 changes: 23 additions & 0 deletions src/neo4j/graph/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,21 @@ def relationship_type(self, name: str) -> type[Relationship]:
)
return cls

def __reduce__(self):
state = self.__dict__.copy()
relationship_types = tuple(state.pop("_relationship_types", {}).keys())
restore_args = (relationship_types,)
return Graph._restore, restore_args, state

@staticmethod
def _restore(relationship_types: tuple[str, ...]) -> Graph:
graph = Graph().__new__(Graph)
graph.__dict__["_relationship_types"] = {
name: type(str(name), (Relationship,), {})
for name in relationship_types
}
return graph


class Entity(t.Mapping[str, t.Any]):
"""
Expand Down Expand Up @@ -287,6 +302,14 @@ def type(self) -> str:
"""
return type(self).__name__

def __reduce__(self):
return Relationship._restore, (self.graph, self.type), self.__dict__

@staticmethod
def _restore(graph: Graph, name: str) -> Relationship:
type_ = graph.relationship_type(name)
return type_.__new__(type_)


class Path:
"""Self-contained graph path."""
Expand Down
14 changes: 14 additions & 0 deletions tests/unit/common/graph/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Copyright (c) "Neo4j"
# Neo4j Sweden AB [https://neo4j.com]
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
Loading