Closed as duplicate
Closed as duplicate
Description
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
Metadata
Metadata
Assignees
Labels
Projects
Status
Todo