GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Incompatible (wrt floats) processing of alignment and '0' flag in Fraction's formatting · Issue #130716 · python/cpython

Incompatible (wrt floats) processing of alignment and '0' flag in Fraction's formatting #130716

New issue
Copy link
New issue
Copy link
Closed as duplicate
Closed as duplicate
Incompatible (wrt floats) processing of alignment and '0' flag in Fraction's formatting#130716
Copy link
Labels
docsDocumentation in the Doc dirDocumentation in the Doc dirinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@skirpichev
skirpichev
opened on Mar 1, 2025
Issue body actions

Bug report

Bug description:

>>> x = Fraction(1, 3)
>>> f"{x:_>010f}"
Traceback (most recent call last):
  File "<python-input-2>", line 1, in <module>
    f"{x:_>010f}"
      ^^^^^^^^^^
  File "/home/sk/src/cpython/Lib/fractions.py", line 600, in __format__
    raise ValueError(
    ...<2 lines>...
    )
ValueError: Invalid format specifier '_>010f' for object of type 'Fraction'
>>> f"{float(x):_>010f}"
'__0.333333'

While not being documented, this behavior is intentional (see #100161): "The Fraction formatter is also stricter about combining these two things: it's not permitted to use both the '0' flag and explicit alignment, on the basis that we should refuse the temptation to guess in the face of ambiguity." In floats case, "0" flag is just ignored if the fill character (and, thus, alignment) already specified.

Maybe it's not a bad idea to use Fraction's behavior for floats too, i.e. deprecate this silent resolution of ambiguity. Or just document this discrepancy.

Decimal's behave here like Fraction's:

>>> f"{Decimal(1.25):_>010f}"
Traceback (most recent call last):
  File "<python-input-5>", line 1, in <module>
    f"{Decimal(1.25):_>010f}"
      ^^^^^^^^^^^^^^^^^^^^^^
ValueError: invalid format string

CPython versions tested on:

CPython main branch

Operating systems tested on:

No response

Activity

  1. skirpichev
    added
    docsDocumentation in the Doc dirDocumentation in the Doc dir
    interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Mar 1, 2025
  2. skirpichev
    self-assigned this
    on Mar 1, 2025
  3. github-project-automation
    added this to docs issueson Mar 1, 2025
  4. github-project-automation
    moved this to Todo in docs issueson Mar 1, 2025
  5. skirpichev
    removed their assignment
    on Apr 6, 2025
  6. skirpichev
    closed this as duplicateon Apr 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsDocumentation in the Doc dirDocumentation in the Doc dirinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

    Projects

    • docs issues
      Status
      Todo
      Show more project fields

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402503 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.