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

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

Copy link
Copy Markdown
Contributor

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?

@emilyklemilykl assigned RazerM and unassigned emilyklApr 14, 2026
@RazerM

Copy link
Copy Markdown
ContributorAuthor

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.

@camdecostercamdecoster assigned emilykl and unassigned RazerMApr 15, 2026
@emilykl

Copy link
Copy Markdown
Contributor

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).

Good point, that's a solid argument in favor. Thanks for the other examples as well. I'll approve!

@emilyklemilykl 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:mainApr 15, 2026
24 checks passed
Sign up for freeto 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

@RazerM@emilykl@camdecoster