Skip to content

[Python] Deprecate TObject __eq__ pythonization#19145

Merged
guitargeek merged 1 commit intoroot-project:masterfrom
guitargeek:tobject_pyz
Oct 23, 2025
Merged

[Python] Deprecate TObject __eq__ pythonization#19145
guitargeek merged 1 commit intoroot-project:masterfrom
guitargeek:tobject_pyz

Conversation

@guitargeek
Copy link
Copy Markdown
Contributor

@guitargeek guitargeek commented Jun 23, 2025

The TObject __eq__ pythonization calls TObject::Equals(), which by default does a pointer comparison. That can be very confusing for Python users.

Here is how the current warning will look like:

/home/rembserj/code/root/root_build/../chck.py:11: FutureWarning:
TObject.__eq__ is deprecated and will be removed in ROOT 6.40.

It forwards to TObject::Equals(), which uses pointer comparison if not overridden in derived classes.
This may be confusing, because people expect value comparisons.
Use Pythons `is` for pointer comparison, or request/implement `operator==` on the C++ side if you need value-based equality checks.
  print(n1 == n1)
True

@vepadulano
Copy link
Copy Markdown
Member

Just a fly-by thought, this pythonization has been around for so many years that, while I still strongly believe its current implementation is buggy, I am afraid this will introduce a backwards-incompatible behaviour far, far worse than #19061. One possible way to mitigate this is to have a check that if there is no operator == implementation (or equivalent) for a class, then we can raise an informative exception. Actually, we should probably just raise a warning and declare a deprecation phase for this. Also, note that while the discussion at #19061 is regarding TH1 classes and derived only, this PR is going to have effects for every TObject derived class.

@github-actions
Copy link
Copy Markdown

Test Results

    20 files      20 suites   3d 19h 51m 59s ⏱️
 3 043 tests  3 043 ✅ 0 💤 0 ❌
59 159 runs  59 159 ✅ 0 💤 0 ❌

Results for commit 37f4365.

@guitargeek
Copy link
Copy Markdown
Contributor Author

guitargeek commented Jun 24, 2025

That's exactly my intention! I wanted to see what the CI says, and if there are no problems deprecate this Pythonization.

I am afraid this will introduce a backwards-incompatible behaviour far, far worse than #19061.

In my opinion, the silent behavior changes that are far worse. Removing a method like suggested by this PR at least gives you an error (or warning after this PR is updated).

You also have to see the picture with ROOT 6.34 before the UHI introduction in 6.36. What you guys did with the new __eq__ was to introduce a change of behavior without documenting it, and as you can see from our tests and #19035 this even confused our own code. So it definitely has the potential to create bugs in user code. Doesn't matter how wrong or "buggy" you think the old behavior of comparing by pointer was: it has been there for many years as you said and people might rely on it.

Also, note that while the discussion at #19061 is regarding TH1 classes and derived only, this PR is going to have effects for every TObject derived class.

Yes, and this is a bonus 🙂

@vepadulano
Copy link
Copy Markdown
Member

Yes, and this is a bonus 🙂

Only if we make sure this won't disrupt users' code. As things stand, this PR cannot be merged. My suggestion is to start a deprecation phase with a warning, then make it an exception or not implemented in a later ROOT release like 6.40 or 6.42

@vepadulano vepadulano assigned guitargeek and unassigned vepadulano Jul 8, 2025
@guitargeek guitargeek changed the title [Python] Remove TObject __eq__ pythonization [Python] Deprecate TObject __eq__ pythonization Oct 22, 2025
@guitargeek guitargeek marked this pull request as ready for review October 22, 2025 20:34
@guitargeek
Copy link
Copy Markdown
Contributor Author

My suggestion is to start a deprecation phase with a warning, then make it an exception or not implemented in a later ROOT release like 6.40 or 6.42

Good idea! I have implemented what you suggested, and also added an entry in the release notes.

Copy link
Copy Markdown
Member

@vepadulano vepadulano left a comment

Choose a reason for hiding this comment

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

Great thank you! Let's see if it doesn't break any existing test in the CI, then this can be merged 👍

The TObject `__eq__` pythonization calls `TObject::Equals()`, which by
default does a pointer comparison. That can be very confusing for Python
users.
@guitargeek guitargeek requested a review from pcanal as a code owner October 23, 2025 00:10
@guitargeek guitargeek merged commit 70e67f1 into root-project:master Oct 23, 2025
23 of 26 checks passed
@guitargeek guitargeek deleted the tobject_pyz branch October 23, 2025 02:17
guitargeek added a commit to guitargeek/HiggsAnalysis-CombinedLimit that referenced this pull request Oct 28, 2025
This is bad practice in Python, because it only works if the type of the
operand implements the equality operator.

In particular, it will fail with newer ROOT versions, because the
`TObject` base class won't implement the equality operator anymore:
root-project/root#19145
guitargeek added a commit to guitargeek/HiggsAnalysis-CombinedLimit that referenced this pull request Oct 28, 2025
This is bad practice in Python, because it only works if the type of the
operand implements the equality operator.

In particular, it will fail with newer ROOT versions, because the
`TObject` base class won't implement the equality operator anymore:
root-project/root#19145

This commit was done automatically with code modernization tools.
guitargeek added a commit to guitargeek/HiggsAnalysis-CombinedLimit that referenced this pull request Oct 28, 2025
This is bad practice in Python, because it only works if the type of the
operand implements the equality operator.

In particular, it will fail with newer ROOT versions, because the
`TObject` base class won't implement the equality operator anymore:
root-project/root#19145

This commit was done automatically with code modernization tools.
maxgalli pushed a commit to cms-analysis/HiggsAnalysis-CombinedLimit that referenced this pull request Oct 29, 2025
This is bad practice in Python, because it only works if the type of the
operand implements the equality operator.

In particular, it will fail with newer ROOT versions, because the
`TObject` base class won't implement the equality operator anymore:
root-project/root#19145

This commit was done automatically with code modernization tools.
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.

2 participants