Skip to content

Fix error messages in binary/ternary ops to match CPython format#7625

Merged
youknowone merged 2 commits intoRustPython:mainfrom
changjoon-park:fix-divmod-error-message
Apr 19, 2026
Merged

Fix error messages in binary/ternary ops to match CPython format#7625
youknowone merged 2 commits intoRustPython:mainfrom
changjoon-park:fix-divmod-error-message

Conversation

@changjoon-park
Copy link
Copy Markdown
Contributor

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

Fixes two closely-related error-message divergences in crates/vm/src/vm/vm_ops.rs. Both affect TypeError text for arithmetic ops with unsupported operand types. Each unmasks one @unittest.expectedFailure in test_fractions.py.

Fix 1 — divmod missing parentheses

Symptom. divmod(x, y) with unsupported types emits "... for divmod: ...". CPython emits "... for divmod(): ..." (the function-call form, consistent with how pow() is named).

>>> divmod(1.5, 1j)
# CPython:    TypeError: unsupported operand type(s) for divmod(): 'float' and 'complex'
# RustPython: TypeError: unsupported operand type(s) for divmod:   'float' and 'complex'

Fix. vm_ops.rs:404"divmod""divmod()".

Source. CPython's Objects/abstract.c uses "divmod()" as the op_name for the divmod dispatch.

Unmasks: test_fractions.py::FractionTest::test_complex_handling.

Fix 2 — ternary op error uses " and " instead of ", "

Symptom. 3-argument error (e.g. pow(a, b, c) with unsupported types) emits "'X' and 'Y', 'Z'". CPython emits "'X', 'Y', 'Z'" — all commas.

>>> pow(Fraction(3), 4, 5)
# CPython:    TypeError: unsupported operand type(s) for ** or pow(): 'Fraction', 'int', 'int'
# RustPython: TypeError: unsupported operand type(s) for ** or pow(): 'Fraction' and 'int', 'int'

Fix. vm_ops.rs:375 — the format string's '{}' and '{}', '{}''{}', '{}', '{}'.

The 2-argument case (line 367) correctly keeps 'X' and 'Y' (CPython matches this).

Unmasks: test_fractions.py::FractionTest::test_three_argument_pow.

Verification

$ ./target/release/rustpython -m test test_fractions -v
test_complex_handling     ... ok     # was: expected failure
test_three_argument_pow   ... ok     # was: expected failure
Ran 50 tests in 0.060s
OK                                    # was: OK (expected failures=2)

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 18, 2026

📝 Walkthrough

Walkthrough

Adjusted VM error-message formatting for a ternary operand type error to include the third operand's type and tweaked the Divmod operator description string from "divmod" to "divmod()". No control flow or dispatch behavior was changed.

Changes

Cohort / File(s) Summary
Ternary operand error formatting
crates/vm/src/vm/vm_ops.rs
Modified the constructed type-error message for unsupported ternary operand types to include the third operand's class (c.class()) and changed format placeholders to list three types.
Operator description update
crates/vm/src/vm/vm_ops.rs
Changed the Divmod operator description string from "divmod" to "divmod()" (affects wording in related error messages).

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~2 minutes

Poem

🐰 A tiny tweak in the VM's song,
I added a type and fixed a name so long.
Three types now listed, clear and bright,
divmod() hops into the light. 🥕✨

🚥 Pre-merge checks | ✅ 2 | ❌ 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 (2 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: fixing error messages in binary/ternary operations to match CPython format, which is the primary focus of the changeset.

✏️ 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

github-actions bot commented Apr 18, 2026

📦 Library Dependencies

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

[x] lib: cpython/Lib/fractions.py
[x] test: cpython/Lib/test/test_fractions.py

dependencies:

  • fractions

dependent tests: (65 tests)

  • fractions: test_buffer test_builtin test_compare test_float test_fractions test_itertools test_math test_numeric_tower test_operator test_os test_random test_statistics test_string
    • statistics: test_signal
      • random: test_asyncio test_bisect test_bz2 test_collections test_complex test_context test_dbm_dumb test_decimal test_deque test_descr test_devpoll test_dict test_dummy_thread test_email test_functools test_heapq test_hmac test_importlib test_int test_io test_logging test_long test_lzma test_mmap test_ordered_dict test_poll test_posixpath test_pow test_pprint test_queue test_regrtest test_richcmp test_selectors test_set test_shutil test_socket test_sort test_strtod test_struct test_sys test_tarfile test_thread test_threading test_tokenize test_traceback test_unparse test_uuid test_weakref test_zipfile test_zlib test_zstd

Legend:

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

@changjoon-park changjoon-park changed the title Fix divmod error message to match CPython format Fix error messages in binary/ternary ops to match CPython format Apr 18, 2026
Copy link
Copy Markdown
Member

@youknowone youknowone 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 67eeddd into RustPython:main Apr 19, 2026
21 checks passed
@changjoon-park changjoon-park deleted the fix-divmod-error-message branch April 19, 2026 04:12
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