Skip to content

Commit

Permalink
Simplify Pipeline.__eq__ logic (deepset-ai#6840)
Browse files Browse the repository at this point in the history
  • Loading branch information
silvanocerza authored Jan 29, 2024
1 parent acf4cd5 commit b1ec32d
Showing 1 changed file with 2 additions and 25 deletions.
27 changes: 2 additions & 25 deletions haystack/core/pipeline/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,9 @@ def __eq__(self, other) -> bool:
Equal pipelines share every metadata, node and edge, but they're not required to use
the same node instances: this allows pipeline saved and then loaded back to be equal to themselves.
"""
if (
not isinstance(other, type(self))
or not getattr(self, "metadata") == getattr(other, "metadata")
or not getattr(self, "max_loops_allowed") == getattr(other, "max_loops_allowed")
or not hasattr(self, "graph")
or not hasattr(other, "graph")
):
if not isinstance(other, Pipeline):
return False

return (
self.graph.adj == other.graph.adj
and self._comparable_nodes_list(self.graph) == self._comparable_nodes_list(other.graph)
and self.graph.graph == other.graph.graph
)
return self.to_dict() == other.to_dict()

def to_dict(self) -> Dict[str, Any]:
"""
Expand Down Expand Up @@ -172,18 +161,6 @@ def from_dict(cls: Type[T], data: Dict[str, Any], **kwargs) -> T:

return pipe

def _comparable_nodes_list(self, graph: networkx.MultiDiGraph) -> List[Dict[str, Any]]:
"""
Replaces instances of nodes with their class name in order to make sure they're comparable.
"""
nodes = []
for node in graph.nodes:
comparable_node = graph.nodes[node]
comparable_node["instance"] = comparable_node["instance"].__class__
nodes.append(comparable_node)
nodes.sort()
return nodes

def add_component(self, name: str, instance: Component) -> None:
"""
Create a component for the given component. Components are not connected to anything by default:
Expand Down

0 comments on commit b1ec32d

Please sign in to comment.