Skip to content

Drop int() delegation to __trunc__#7703

Merged
youknowone merged 1 commit intoRustPython:mainfrom
changjoon-park:fix-int-trunc-delegation
Apr 28, 2026
Merged

Drop int() delegation to __trunc__#7703
youknowone merged 1 commit intoRustPython:mainfrom
changjoon-park:fix-int-trunc-delegation

Conversation

@changjoon-park
Copy link
Copy Markdown
Contributor

@changjoon-park changjoon-park commented Apr 27, 2026

Background

CPython 3.14 fully removed the long-deprecated delegation from int() to __trunc__. Classes that want to support int(x) must implement __int__ or __index__; math.trunc(x) continues to use __trunc__.

RustPython still delegated to __trunc__ (with a DeprecationWarning) inside PyObject::try_int, so int(JustTrunc()) returned the truncated value instead of raising TypeError.

Repro

class JustTrunc:
    def __trunc__(self): return 42

int(JustTrunc())
# CPython 3.14:    TypeError: int() argument must be a string, a bytes-like object or a real number, not 'JustTrunc'
# RustPython:      42 (with DeprecationWarning)   (before this PR)

Fix

Delete the __trunc__ branch in PyObject::try_int (crates/vm/src/protocol/number.rs). The fall-through reaches the existing TypeError arm.

math.trunc() is unaffected — it goes through the __trunc__ method directly, not through try_int.

Tests unmasked

  • test_int.IntTestCases.test_intconversion (covers int(JustTrunc()) raising TypeError)

Verification

  • CPython 3.14.4 byte-identical for int(JustTrunc()) (TypeError), int(IntOverridesTrunc()) (uses __int__ → 42), math.trunc(T()) (still uses __trunc__ → 100)
  • No regressions across 8 modules (~2,022 tests): test_int, test_math, test_decimal, test_descr, test_class, test_inspect, test_typing, test_unittest.testmock.testmagicmethods

Summary by CodeRabbit

  • Changes
    • Removed __trunc__ method delegation from integer conversion. The int() function will no longer attempt to use the __trunc__ method on objects.
    • Removed associated deprecation warnings previously issued during integer conversion.

CPython 3.14 fully removed the long-deprecated fallback from int() to
__trunc__. Classes that want int(x) support must implement __int__ or
__index__; math.trunc(x) continues to use __trunc__.

Delete the __trunc__ branch in PyObject::try_int so a class with only
__trunc__ now reaches the existing TypeError arm, matching CPython.

Unmasks test_int.IntTestCases.test_intconversion.
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 27, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

Run ID: 71cd4799-fa94-4a60-bea2-3a3e5c67b09d

📥 Commits

Reviewing files that changed from the base of the PR and between 1d42ee5 and 82816b8.

⛔ Files ignored due to path filters (1)
  • Lib/test/test_int.py is excluded by !Lib/**
📒 Files selected for processing (1)
  • crates/vm/src/protocol/number.rs
💤 Files with no reviewable changes (1)
  • crates/vm/src/protocol/number.rs

📝 Walkthrough

Walkthrough

The PyObject::try_int method removes support for delegating to the __trunc__ special method during integer conversion. The code that checked for __trunc__, emitted deprecation warnings, and invoked the method is now deleted, simplifying the conversion logic.

Changes

Cohort / File(s) Summary
Integer Conversion Deprecation Removal
crates/vm/src/protocol/number.rs
Removes 14 lines of code that delegated int() conversion to __trunc__ method, including deprecation warning and return value validation logic.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~5 minutes

Poem

🐰 A deprecation fades away,
__trunc__ has had its day,
Simpler paths now pave the way,
For int() conversions—hooray! ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately and directly describes the main change: removing int() delegation to trunc in the codebase, matching the PR's primary objective of aligning with CPython 3.14 behavior.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions
Copy link
Copy Markdown
Contributor

📦 Library Dependencies

The following Lib/ modules were modified. Here are their dependencies:

[x] lib: cpython/Lib/_pylong.py
[ ] test: cpython/Lib/test/test_int.py (TODO: 4)
[x] test: cpython/Lib/test/test_long.py (TODO: 4)
[x] test: cpython/Lib/test/test_int_literal.py

dependencies:

  • int

dependent tests: (no tests depend on int)

Legend:

  • [+] path exists in CPython
  • [x] up-to-date, [ ] outdated

Copy link
Copy Markdown
Contributor

@ShaharNaveh ShaharNaveh left a comment

Choose a reason for hiding this comment

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

🚀

@youknowone youknowone merged commit e5f2d2d into RustPython:main Apr 28, 2026
21 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.

3 participants