diff --git a/CHANGELOG.md b/CHANGELOG.md index 2f31a86d2d3..b1007831fe4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ This project adheres to [Semantic Versioning](http://semver.org/). ### Fixed - Update tests to be compatible with numpy 2.4 [[#5522](https://github.com/plotly/plotly.py/pull/5522)], with thanks to @thunze for the contribution! +### Updated +- The `__eq__` method for `graph_objects` classes now returns `NotImplemented` to give the other operand an opportunity to handle the comparison [[#5547](https://github.com/plotly/plotly.py/pull/5547)], with thanks to @RazerM for the contribution! ## [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