diff --git a/CHANGELOG.md b/CHANGELOG.md index 9d04f16552c..b1f08e50bd5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,7 @@ All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/). ## Unreleased - +- The `__eq__` method for `graph_objects` classes now returns `NotImplemented` to give the other operand an opportunity to handle the comparison. ## [6.7.0] - 2026-04-09 diff --git a/plotly/basedatatypes.py b/plotly/basedatatypes.py index 6821eeb8d09..ec4038b7fa4 100644 --- a/plotly/basedatatypes.py +++ b/plotly/basedatatypes.py @@ -789,7 +789,7 @@ def __contains__(self, prop): def __eq__(self, other): if not isinstance(other, BaseFigure): # Require objects to both be BaseFigure instances - return False + return NotImplemented else: # Compare plotly_json representations @@ -5017,7 +5017,7 @@ def __eq__(self, other): """ if not isinstance(other, self.__class__): # Require objects to be of the same plotly type - return False + return NotImplemented else: # Compare plotly_json representations diff --git a/tests/test_core/test_graph_objs/test_figure_properties.py b/tests/test_core/test_graph_objs/test_figure_properties.py index d7847a58764..9676137397c 100644 --- a/tests/test_core/test_graph_objs/test_figure_properties.py +++ b/tests/test_core/test_graph_objs/test_figure_properties.py @@ -1,4 +1,5 @@ from unittest import TestCase +from unittest.mock import MagicMock import pytest import plotly.graph_objs as go @@ -42,6 +43,15 @@ def test_contains(self): def test_iter(self): self.assertEqual(set(self.figure), {"data", "layout", "frames"}) + def test_unsupported_eq_returns_not_implemented(self): + other = MagicMock() + self.assertFalse(self.figure == other) + other.__eq__.assert_called_once_with(self.figure) + + other.reset_mock() + self.assertFalse(self.figure.layout == other) + other.__eq__.assert_called_once_with(self.figure.layout) + def test_attr_item(self): # test that equal objects can be retrieved using attr or item # syntax