Skip to content

Drop redundant exception type prefix from float-to-int error messages#7763

Merged
youknowone merged 1 commit into
RustPython:mainfrom
changjoon-park:fix-float-to-int-error-prefix
May 4, 2026
Merged

Drop redundant exception type prefix from float-to-int error messages#7763
youknowone merged 1 commit into
RustPython:mainfrom
changjoon-park:fix-float-to-int-error-prefix

Conversation

@changjoon-park

@changjoon-park changjoon-park commented May 2, 2026

Copy link
Copy Markdown
Contributor

Summary

CPython raises OverflowError("cannot convert float infinity to integer") and ValueError("cannot convert float NaN to integer") (Objects/floatobject.c). The exception type name is added by Python's traceback display layer; the message itself should not duplicate it.

try_to_bigint was producing the duplicated form:

>>> int(float("inf"))
# CPython 3.14.4: OverflowError('cannot convert float infinity to integer')
# RustPython main: OverflowError('OverflowError: cannot convert float infinity to integer')  ❌

This makes repr(e) and any code path that inspects str(e) diverge from CPython.

Affected sites

All five callers of try_to_bigint share the message:

Operation Caller
int(x) float.__int__
math.floor(x) float.__floor__
math.ceil(x) float.__ceil__
math.trunc(x) float.__trunc__
round(x) (no ndigits) float.__round__

Verification

  • Byte-identical with CPython 3.14.4 across 14 probe sites: 14/14 match.
  • cargo run --release -- -m test test_float test_builtin test_math test_int test_long test_complex test_decimal test_fractions test_statistics test_format test_re — 1,627 tests pass, 0 regressions.
  • All 188 extra_tests/snippets/*.py pass under the CI feature set.

A regression block is added to extra_tests/snippets/builtin_float.py checking the exact message text via a small _check_msg(call, exc_type, expected_msg) helper across all five callers.

Summary by CodeRabbit

Release Notes

  • Bug Fixes

    • Improved error messages when converting float infinity and NaN values to integers with standardized wording for consistency.
  • Tests

    • Added comprehensive test coverage for float-to-integer conversion error messages across multiple conversion methods.

CPython raises `OverflowError("cannot convert float infinity to integer")`
and `ValueError("cannot convert float NaN to integer")` from
`Objects/floatobject.c::float___trunc___impl` and friends. The exception
type name is added by Python's traceback display layer; the message
itself should not duplicate it.

`try_to_bigint` was producing
`OverflowError("OverflowError: cannot convert ...")` etc., which made
`repr(e)` and any code path that inspects `str(e)` diverge from CPython.

Affects all 5 callers of `try_to_bigint`: `__int__`, `__floor__`,
`__ceil__`, `__round__` (no-arg), `__trunc__` — i.e. `int(x)`,
`math.floor/ceil/trunc(x)`, `round(x)` for non-finite floats.

Verified byte-identical with CPython 3.14.4 across 14 affected sites.
@coderabbitai

coderabbitai Bot commented May 2, 2026

Copy link
Copy Markdown
Contributor

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: 3ef08313-1ca3-4978-8344-d33dd6ea6600

📥 Commits

Reviewing files that changed from the base of the PR and between 926d69b and 32c9bf1.

📒 Files selected for processing (2)
  • crates/vm/src/builtins/float.rs
  • extra_tests/snippets/builtin_float.py

📝 Walkthrough

Walkthrough

Error messages for float-to-integer conversion failures are standardized to match CPython's exact wording. The try_to_bigint function removes error prefix strings and updates messages for infinity and NaN cases. Tests verify the standardized messages.

Changes

Float Conversion Error Standardization

Layer / File(s) Summary
Error Message Update
crates/vm/src/builtins/float.rs
try_to_bigint removes "OverflowError:" and "ValueError:" prefixes; updates messages to "cannot convert float infinity to integer" and "cannot convert float NaN to integer".
Error Message Verification
extra_tests/snippets/builtin_float.py
New _check_msg helper validates exact exception message text; assertions verify CPython-identical messages for int(), round(), math.floor/ceil/trunc, and dunder methods with INF, -INF, and NAN.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Suggested reviewers

  • youknowone
  • ShaharNaveh

Poem

🐰 Floats that soar to endless skies,
Now speak in standardized cries,
With messages clear, no prefix fuss,
CPython's tongue is ours to trust!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: removing redundant exception type prefixes from float-to-int error messages, which aligns with the core objective and file modifications.
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
Review rate limit: 7/8 reviews remaining, refill in 7 minutes and 30 seconds.

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

@ShaharNaveh ShaharNaveh 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.

huh, good catch!

@changjoon-park

Copy link
Copy Markdown
Contributor Author

huh, good catch!

thanks for the review !

@youknowone youknowone left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

👍

@youknowone

Copy link
Copy Markdown
Member

@changjoon-park Just out of curiosity, are you finding bugs using fuzzing or other automated tools?

@youknowone youknowone merged commit 181e4e7 into RustPython:main May 4, 2026
20 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