Skip to content

Commit 0982e25

Browse files
authored
Merge pull request #5547 from RazerM/feature/eq-return-not-implemented
Return NotImplemented for unsupported comparisons
2 parents 3046c71 + aa4fbac commit 0982e25

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
88
- Fix issue where user-specified `color_continuous_scale` was ignored when template had `autocolorscale=True` [[#5439](https://github.com/plotly/plotly.py/pull/5439)], with thanks to @antonymilne for the contribution!
99
- 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!
1010

11+
### Updated
12+
- 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!
1113

1214
## [6.7.0] - 2026-04-09
1315

plotly/basedatatypes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -789,7 +789,7 @@ def __contains__(self, prop):
789789
def __eq__(self, other):
790790
if not isinstance(other, BaseFigure):
791791
# Require objects to both be BaseFigure instances
792-
return False
792+
return NotImplemented
793793
else:
794794
# Compare plotly_json representations
795795

@@ -5017,7 +5017,7 @@ def __eq__(self, other):
50175017
"""
50185018
if not isinstance(other, self.__class__):
50195019
# Require objects to be of the same plotly type
5020-
return False
5020+
return NotImplemented
50215021
else:
50225022
# Compare plotly_json representations
50235023

tests/test_core/test_graph_objs/test_figure_properties.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from unittest import TestCase
2+
from unittest.mock import MagicMock
23
import pytest
34

45
import plotly.graph_objs as go
@@ -42,6 +43,15 @@ def test_contains(self):
4243
def test_iter(self):
4344
self.assertEqual(set(self.figure), {"data", "layout", "frames"})
4445

46+
def test_unsupported_eq_returns_not_implemented(self):
47+
other = MagicMock()
48+
self.assertFalse(self.figure == other)
49+
other.__eq__.assert_called_once_with(self.figure)
50+
51+
other.reset_mock()
52+
self.assertFalse(self.figure.layout == other)
53+
other.__eq__.assert_called_once_with(self.figure.layout)
54+
4555
def test_attr_item(self):
4656
# test that equal objects can be retrieved using attr or item
4757
# syntax

0 commit comments

Comments
 (0)