Skip to content

Match invalid format specifier error messages - #8477

Merged
youknowone merged 2 commits into
RustPython:mainfrom
shAn-kor:fix-invalid-format-specifier-error-message
Oct 5, 2026
Merged

youknowone merged 2 commits 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 format-specification handling for booleans, integers, floating-point numbers, complex numbers, and strings.
    • Invalid format specifications now produce consistent errors that identify the specification and affected value type.
    • Formatting errors retain their appropriate error types and messages.
    • Existing locale-aware, standard, and empty-spec formatting behavior remains unchanged.

@coderabbitai

coderabbitai Bot commented Aug 9, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

🧰 Additional context used
📚 Code guidelines (1)
AGENTS.md — auto-discovered

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository: RustPython/RustPython/.coderabbit.yml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 5754181a-54e1-435d-993f-37447fb586cf
📥 Commits

Reviewing files that changed from the base of the PR and between dd16a10 and ebde064.

⛔ 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

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.


📝 Walkthrough

Walkthrough

Built-in boolean, complex, float, integer, and string formatting methods now use shared helpers to parse format specifications and convert formatting errors. Invalid-specifier errors include the specification and object type.

Changes

Format-spec parsing

Layer / File(s) Summary
Shared format-spec parser
crates/vm/src/format.rs
Adds parse_format_spec and format_spec_error to parse specifications and map errors to Python exceptions. Invalid-specifier errors include the specification and object type.
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 helpers instead of direct FormatSpec parsing and prior exception conversion. Existing formatting paths remain in place.

Priority: ➖ Normal

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

Change: Bug fix

Suggested reviewers: leehanjeong

Merge Risk: ⚪ Minimal · up to ebde0

This change makes invalid format specifier errors more detailed and consistent with CPython. No concrete merge-blocking risk was identified.

Security Architecture Review

Security architecture risk: ⚪ Minimal · up to ebde0

The change improves invalid-format diagnostics without expanding access or weakening the inspected formatting safeguards. No material security issue was identified in the change.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — The confirmed exposure is the existing formatting operations within an executing interpreter and their returned exceptions. Their new shared adapter introduces neither an additional public invocation path nor a privileged operation.

Trust Boundaries and Controls

  • observed — Caller-controlled specifier text and potentially caller-defined type names are interpolated into the returned ValueError. The new class-name lookup reads stored VM metadata rather than invoking Python attribute access or methods; these values do not select authority or a sensitive operation in the helper.

Resilience and Maintainability Implications

  • inferred — The new helpers borrow their inputs and return a parsed specification or a newly constructed exception. They introduce no shared mutable state or durable multi-step transition requiring recovery, rollback, or repeated-call coordination.
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 75.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 8 functions across 6 files. 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 CPython-style error messages for invalid format specifiers.
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.
  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR

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)

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
youknowone
youknowone previously approved these changes Aug 11, 2026
@youknowone
youknowone dismissed their stale review August 11, 2026 12:22

let me think once again if this is the best way

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

Hi! I left a change request not to make confusion to old path.
Sorry for late review!

Comment thread crates/vm/src/format.rs
}
}

pub(crate) fn parse_format_spec(

@youknowone youknowone Aug 18, 2026 •

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.

due to this helper, FormatSpecError::InvalidFormatSpecifier arm in impl IntoPyException for FormatSpecError is now a trap. The error message will be diverged by its calling path.
if we go this direction, I prefer to remove impl IntoPyException for FormatSpecError and bring the implementation into this helper.

shAn-kor and others added 2 commits October 5, 2026 23:10
Assisted-by: Codex:gpt-5.6-sol
InvalidFormatSpecifier needs the spec and the object type. Drop
IntoPyException so that path cannot produce a generic message.

Assisted-by: Grok:4.6
@youknowone
youknowone force-pushed the fix-invalid-format-specifier-error-message branch from dd16a10 to ebde064 Compare October 5, 2026 14:37
@youknowone
youknowone enabled auto-merge (squash) October 5, 2026 15:25
@codspeed

codspeed Bot commented Oct 5, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 62 untouched benchmarks
⏩ 4 skipped benchmarks1


Comparing shAn-kor:fix-invalid-format-specifier-error-message (ebde064) with main (524f1a2)

Open in CodSpeed

Footnotes

  1. 4 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@youknowone
youknowone merged commit 3d1c174 into RustPython:main Oct 5, 2026
31 checks passed
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.

3 participants