Skip to content

Return NotImplemented for unsupported comparisons#5547

Open
RazerM wants to merge 6 commits intoplotly:mainfrom
RazerM:feature/eq-return-not-implemented
Open

Return NotImplemented for unsupported comparisons#5547
RazerM wants to merge 6 commits intoplotly:mainfrom
RazerM:feature/eq-return-not-implemented

Conversation

@RazerM
Copy link
Copy Markdown

@RazerM RazerM commented Mar 27, 2026

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
Copy link
Copy Markdown
Contributor

emilykl commented Apr 14, 2026

Hi @RazerM, thanks for the contribution, and thanks for your patience awaiting review!

I'm trying to make sure I understand the consequences of this change.

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

I'm having trouble thinking of a scenario where that would be helpful. It's possibly something that could be useful to us internally within plotly.py, but we haven't encountered a need for it yet to my knowledge. Do you have a scenario on your end that would benefit from this change?

@emilykl emilykl assigned RazerM and unassigned emilykl Apr 14, 2026
@RazerM
Copy link
Copy Markdown
Author

RazerM commented Apr 14, 2026

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.

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