diff --git a/CHANGELOG.md b/CHANGELOG.md index 941722802c5..0f71737a581 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). ### Fixed - Update `numpy.percentile` syntax to stop using deprecated alias [[5483](https://github.com/plotly/plotly.py/pull/5483)], with thanks to @Mr-Neutr0n for the contribution! - `numpy` with a version less than 1.22 is no longer supported. +- The `__eq__` method for `graph_objects` classes now returns `NotImplemented` to give the other operand an opportunity to handle the comparison. ## [6.6.0] - 2026-03-02 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