Skip to content

Match invalid format specifier error messages - #8477

Open
shAn-kor wants to merge 1 commit into
RustPython:mainfrom
shAn-kor:fix-invalid-format-specifier-error-message
Open

Match invalid format specifier error messages#8477
shAn-kor wants to merge 1 commit into
RustPython:mainfrom
shAn-kor:fix-invalid-format-specifier-error-message

Conversation

@shAn-kor

@shAn-kor shAn-kor commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

Summary

This is a follow-up to #8475. RustPython still returned the generic Invalid format specifier message for malformed format specifications, while CPython includes both the original specifier and the runtime object type.

Add a VM-level format-spec parsing helper that maps only InvalidFormatSpecifier to CPython's detailed ValueError message. Integer, boolean, float, complex, and string formatting now use the helper, including subclasses whose runtime type names must appear in the error.

With the error text matching CPython, this enables the existing test_better_error_message_format, test_unicode_in_error_message, and test_specifier_z_error tests without changing their assertions or test data.

Tests

  • cargo run --release -- -m test test_format
  • cargo test --workspace --exclude rustpython_wasm --exclude rustpython-venvlauncher --exclude rustpython-capi
  • (cd crates/capi && cargo test) — 102 passed
  • cargo clippy --workspace --exclude rustpython_wasm --exclude rustpython-venvlauncher --all-targets
  • repository pre-commit and pre-push checks

AI assistance

Codex (GPT-5.6) assisted with comparing CPython and RustPython error behavior, implementing the shared parsing helper, enabling the existing regression tests, running validation, and drafting this PR description. I reviewed the final diff and verified the behavior locally on macOS.

Summary by CodeRabbit

  • Bug Fixes
    • Improved formatting support for booleans, integers, floating-point numbers, complex numbers, and strings.
    • Invalid format specifications now produce clearer, more consistent error messages that identify the affected value type and specification.
    • Preserved existing locale-aware and standard formatting behavior.

Assisted-by: Codex:gpt-5.6-sol
@coderabbitai

coderabbitai Bot commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro Plus

Run ID: cf056c51-a50e-4bb4-94e3-0cfc5d7d88d1

📥 Commits

Reviewing files that changed from the base of the PR and between dca9b09 and 6e4ba86.

⛔ Files ignored due to path filters (1)
  • Lib/test/test_format.py is excluded by !Lib/**
📒 Files selected for processing (6)
  • crates/vm/src/builtins/bool.rs
  • crates/vm/src/builtins/complex.rs
  • crates/vm/src/builtins/float.rs
  • crates/vm/src/builtins/int.rs
  • crates/vm/src/builtins/str.rs
  • crates/vm/src/format.rs

📝 Walkthrough

Walkthrough

The change adds a shared VM-aware format-spec parser. Boolean, complex, float, integer, and string formatting methods now use it for parsing and error conversion.

Changes

Format-spec parsing

Layer / File(s) Summary
Shared format-spec parser
crates/vm/src/format.rs
Adds parse_format_spec, which includes the object type and original specification in invalid-specifier errors.
Built-in formatting integration
crates/vm/src/builtins/bool.rs, crates/vm/src/builtins/complex.rs, crates/vm/src/builtins/float.rs, crates/vm/src/builtins/int.rs, crates/vm/src/builtins/str.rs
Built-in __format__ methods use the shared parser and remove direct FormatSpec imports.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

Suggested reviewers: kangdora, youknowone

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% 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 clearly summarizes the main change: matching invalid format specifier error messages with CPython.
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.
✨ 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.

@github-actions

github-actions Bot commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

📦 Library Dependencies

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

[x] test: cpython/Lib/test/test_format.py

dependencies:

dependent tests: (no tests depend on format)

[x] lib: cpython/Lib/ssl.py
[ ] test: cpython/Lib/test/test_ssl.py (TODO: 10)

dependencies:

  • ssl

dependent tests: (53 tests)

  • ssl: test_asyncio test_ftplib test_httplib test_httpservers test_imaplib test_logging test_poplib test_ssl test_urllib test_urllib2_localnet test_venv test_xmlrpc
    • asyncio.selector_events: test_asyncio
    • ftplib: test_urllib2
      • urllib.request: test_http_cookiejar test_pathlib test_pydoc test_sax test_site test_urllib2net test_urllibnet
    • http.client: test_docxmlrpc test_hashlib test_ucn test_unicodedata test_wsgiref
      • logging.handlers: test_concurrent_futures test_pkgutil
    • http.server: test_robotparser
      • pydoc: test_enum
    • smtplib: test_smtplib test_smtpnet
    • urllib.request:
      • pathlib: test_ast test_dbm_sqlite3 test_ensurepip test_importlib test_json test_launcher test_pathlib test_peg_generator test_pyrepl test_runpy test_tarfile test_tempfile test_tomllib test_tools test_traceback test_unparse test_winapi test_zipapp test_zipfile test_zoneinfo test_zstd

Legend:

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

@moreal moreal added the z-ca-2026 Tag to track Contribution Academy 2026 label Aug 9, 2026
@shAn-kor
shAn-kor marked this pull request as ready for review August 9, 2026 23:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

z-ca-2026 Tag to track Contribution Academy 2026

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants