Implement negative zero coercion formatting by shAn-kor · Pull Request #8475 · RustPython/RustPython · GitHub
Skip to content

Implement negative zero coercion formatting - #8475

Merged
youknowone merged 2 commits into
RustPython:mainfrom
shAn-kor:implement-negative-zero-coercion-format
Aug 9, 2026
Merged

Implement negative zero coercion formatting#8475
youknowone merged 2 commits into
RustPython:mainfrom
shAn-kor:implement-negative-zero-coercion-format

Conversation

@shAn-kor

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

Copy link
Copy Markdown
Contributor

Related to #6839

What changed

RustPython did not support the z format option introduced by PEP 682, which removes the sign from a negative value when its formatted result rounds to zero.

The formatter now parses z after the sign option and applies it to floating point and complex formatting, including the locale-aware n path. The sign is removed only when the formatted magnitude is zero, so -0.0001 formatted with z.2f becomes 0.00 while nonzero negative results keep their sign.

Invalid uses with integer, boolean, and string formats follow CPython error behavior. Complex formatting also preserves explicit + and space signs for purely imaginary values.

The existing test_negative_zero CPython test is now enabled, with additional common-layer coverage for rounding, complex values, locale formatting, and invalid format specifications.

Tests

  • cargo test -p rustpython-common format_negative_zero_coercion
  • cargo run --release -- -m test test_format
  • cargo run --release -- -m test test_complex
  • prek run --all-files
  • repository pre-push checks, including full clippy, workspace tests, and 102 C-API tests

AI assistance

Codex (GPT-5) assisted with comparing CPython and RustPython behavior, drafting the implementation and regression tests, and writing this PR description. I reviewed the final diff and verified the behavior locally.

Summary by CodeRabbit

  • New Features
    • Added support for suppressing negative signs on zero-valued floating-point and complex-number output.
    • Added validation for character formatting options, including precision, signs, alternate form, coercion, and valid code points.
  • Bug Fixes
    • Standardized errors for unsupported formatting options and unknown boolean format types.
    • Added clear type-specific messages when negative-zero suppression is not supported.

@coderabbitai

coderabbitai Bot commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

@github-actions

github-actions Bot commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

@youknowone youknowone added the z-ca-2026 Tag to track Contribution Academy 2026 label Aug 9, 2026

@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
youknowone merged commit 12a0a1e into RustPython:main Aug 9, 2026
28 checks passed
kyokuping pushed a commit to kyokuping/RustPython that referenced this pull request Aug 9, 2026
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