Apply the format spec to a bool instead of dropping it by luantaraschi · Pull Request #8566 · RustPython/RustPython · GitHub
Skip to content

Apply the format spec to a bool instead of dropping it - #8566

Merged
youknowone merged 1 commit into
RustPython:mainfrom
luantaraschi:fix/bool-format-spec
Aug 22, 2026
Merged

youknowone merged 1 commit into
RustPython:mainfrom
luantaraschi:fix/bool-format-spec

Conversation

@luantaraschi

@luantaraschi luantaraschi commented Aug 21, 2026

Copy link
Copy Markdown
Contributor

Summary

A bool ignores every format spec that does not name a presentation type:

>>> f"{True:>5}"
'True'                            # CPython: '    1'
>>> "{:>6}|{:^6}".format(True, False)
'True|False'                      # CPython: '     1|  0   '
>>> format(True, "05")
'True'                            # CPython: '00001'
>>> format(True, ".2")
'True'                            # CPython: ValueError: Precision not allowed in integer format specifier

Width, fill, alignment, sign and the thousands separator are all dropped, and a spec that an integer would reject is accepted quietly. Lining a boolean column up in a table is the case that runs into this, since that spec has no type letter in it.

format(True, "d") and the rest of the presentation types were already right, which is why this only shows up on the specs that leave the letter out.

FormatSpec::format_bool has a None arm for "no presentation type" that returns the spelled out name whatever else the spec holds. CPython does not give bool a __format__ at all:

>>> "__format__" in vars(bool)
False

It inherits int.__format__, and the rule there is the one already written in PyInt::__format__ in this tree: an empty spec on a subclass gives str(self), anything else formats the integer. So the empty spec keeps the old answer and every other spec goes to format_int, which is also what restores Precision not allowed in integer format specifier and the z rejection.

is_empty is written as a destructure of Self rather than a chain of self.field, so that adding a field to FormatSpec later fails to compile here instead of silently making an empty spec look non-empty.

The literal "True" / "False" replaces a round trip through to_string() plus to_uppercase() on the first byte, in the arm that was being rewritten anyway.

Test Plan

Built in a Debian container on rustc 1.98.0.

  • crates/common/src/format.rs gains format_bool_without_a_presentation_type, next to the existing format_bool_basic. Without the change it fails with left: Ok("True") against right: Ok(" 1").
  • extra_tests/snippets/builtin_format.py gains the same ground at the Python level, including the four specs that have to raise. Without the change it stops at assert format(True, "5") == " 1".
  • pytest test_snippets.py -k builtin_format, both legs green, so the file also holds under CPython 3.14.7.
  • -m test test_format, -m test test_bool and -m test test_types on the release build: 18, 31 and 129 tests, all SUCCESS.
  • cargo clippy with the flags CI uses, clean, and cargo fmt --check clean. ruff format --check and ruff check --select I clean on the snippet.
  • cargo test --workspace --exclude rustpython_wasm --exclude rustpython-venvlauncher --exclude rustpython-capi: no failures.

The three clippy jobs and the WASM check are red for the reason in #8564, unrelated to this change.

Summary by CodeRabbit

  • New Features

    • Improved boolean formatting with integer-style alignment, width, zero-padding, signs, and grouping.
    • Added clear True/False output for empty formatting specifications.
  • Bug Fixes

    • Boolean formatting now reports errors for unsupported precision and string-style specifications.
    • Missing presentation types correctly follow integer formatting behavior.
    • Unknown conversion specifiers now produce a clear ValueError message.

@coderabbitai

coderabbitai Bot commented Aug 21, 2026

Copy link
Copy Markdown
Contributor

format_bool answered "True" or "False" for every spec that carries no
presentation type, so width, fill, alignment and sign were all discarded:

    >>> f"{True:>5}"
    'True'

CPython has no bool.__format__ of its own. It uses int's, where an empty
spec on a subclass gives str(self) and everything else formats the integer.
The empty spec keeps the spelled out answer, the rest now goes to
format_int, which also brings back the errors an integer spec raises.

Assisted-by: Claude Code:claude-opus-5
@youknowone
youknowone force-pushed the fix/bool-format-spec branch from c344c34 to 8af4bd3 Compare August 21, 2026 18:12
@youknowone
youknowone merged commit 4c448c4 into RustPython:main Aug 22, 2026
27 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.

2 participants