Return NotImplemented for unsupported comparisons by RazerM · Pull Request #5547 · plotly/plotly.py · GitHub
Skip to content

Return NotImplemented for unsupported comparisons#5547

Merged
emilykl merged 6 commits into
plotly:mainfrom
RazerM:feature/eq-return-not-implemented
Apr 15, 2026
Merged

Return NotImplemented for unsupported comparisons#5547
emilykl merged 6 commits into
plotly:mainfrom
RazerM:feature/eq-return-not-implemented

Conversation

@RazerM

@RazerM RazerM commented Mar 27, 2026

Copy link
Copy Markdown
Contributor

Link to issue

Closes #5546

Description of change

Updated the __eq__ implementations to return NotImplemented instead of False when comparing against an unsupported type. This aligns with Python's data model and allows the other operand to handle the comparison.

Demo

N/A

Testing strategy

Additional information (optional)

https://docs.python.org/3.14/reference/datamodel.html#object.__eq__

Guidelines

@emilykl

emilykl commented Apr 14, 2026

Copy link
Copy Markdown
Contributor

@emilykl emilykl assigned RazerM and unassigned emilykl Apr 14, 2026
@RazerM

RazerM commented Apr 14, 2026

Copy link
Copy Markdown
Contributor Author

we would be allowing the possibility for other objects to declare themselves equal to a BaseFigure (or BasePlotlyType) object, if they choose.

Yes, but they can already do that as long as they're on the LHS. plotly should return NotImplemented so that equality is symmetric (A == B means B == A).

Some use cases are:

  • a test helper such that I can do e.g. assert figure == snapshot. The snapshot object would serialize to a snapshot file if it does not exist, or if it does deserialize it and then return the comparison result. Right now I have to do assert snapshot == figure.

  • SQL query builders etc. overload __eq__. E.g. imagine I have an SQLAlchemy type which can serialize figures. This should work symmetrically:

    select().where(my_figure == MyModel.figure)  # right now this is `.where(False)`!
    select().where(MyModel.figure == my_figure)
    

See also django/django#11808, sympy/sympy#13078 for examples in other popular projects.

@camdecoster camdecoster assigned emilykl and unassigned RazerM Apr 15, 2026
@emilykl

emilykl commented Apr 15, 2026

Copy link
Copy Markdown
Contributor

@emilykl emilykl left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @RazerM !

@emilykl emilykl merged commit 0982e25 into plotly:main Apr 15, 2026
24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG]: Figure.__eq__ returns False for unsupported comparisons

3 participants