Drop int() delegation to __trunc__#7703
Conversation
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.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yml Review profile: CHILL Plan: Pro Run ID: ⛔ Files ignored due to path filters (1)
📒 Files selected for processing (1)
💤 Files with no reviewable changes (1)
📝 WalkthroughWalkthroughThe Changes
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~5 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
📦 Library DependenciesThe following Lib/ modules were modified. Here are their dependencies: [x] lib: cpython/Lib/_pylong.py dependencies:
dependent tests: (no tests depend on int) Legend:
|
Background
CPython 3.14 fully removed the long-deprecated delegation from
int()to__trunc__. Classes that want to supportint(x)must implement__int__or__index__;math.trunc(x)continues to use__trunc__.RustPython still delegated to
__trunc__(with aDeprecationWarning) insidePyObject::try_int, soint(JustTrunc())returned the truncated value instead of raisingTypeError.Repro
Fix
Delete the
__trunc__branch inPyObject::try_int(crates/vm/src/protocol/number.rs). The fall-through reaches the existingTypeErrorarm.math.trunc()is unaffected — it goes through the__trunc__method directly, not throughtry_int.Tests unmasked
test_int.IntTestCases.test_intconversion(coversint(JustTrunc())raisingTypeError)Verification
int(JustTrunc())(TypeError),int(IntOverridesTrunc())(uses__int__→ 42),math.trunc(T())(still uses__trunc__→ 100)test_int,test_math,test_decimal,test_descr,test_class,test_inspect,test_typing,test_unittest.testmock.testmagicmethodsSummary by CodeRabbit
__trunc__method delegation from integer conversion. Theint()function will no longer attempt to use the__trunc__method on objects.