Skip to content

Commit 5bc2d99

Browse files
skirpichevpicnixz
andauthored
gh-130662: Accept leading zeros in precision/width for Fraction's formatting (#130663)
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
1 parent baccfdb commit 5bc2d99

File tree

3 files changed

+7
-8
lines changed

3 files changed

+7
-8
lines changed

Lib/fractions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,9 @@ def _round_to_figures(n, d, figures):
168168
# A '0' that's *not* followed by another digit is parsed as a minimum width
169169
# rather than a zeropad flag.
170170
(?P<zeropad>0(?=[0-9]))?
171-
(?P<minimumwidth>0|[1-9][0-9]*)?
171+
(?P<minimumwidth>[0-9]+)?
172172
(?P<thousands_sep>[,_])?
173-
(?:\.(?P<precision>0|[1-9][0-9]*))?
173+
(?:\.(?P<precision>[0-9]+))?
174174
(?P<presentation_type>[eEfFgG%])
175175
""", re.DOTALL | re.VERBOSE).fullmatch
176176

Lib/test/test_fractions.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1518,6 +1518,8 @@ def test_format_f_presentation_type(self):
15181518
(F(51, 1000), '.1f', '0.1'),
15191519
(F(149, 1000), '.1f', '0.1'),
15201520
(F(151, 1000), '.1f', '0.2'),
1521+
(F(22, 7), '.02f', '3.14'), # issue gh-130662
1522+
(F(22, 7), '005.02f', '03.14'),
15211523
]
15221524
for fraction, spec, expected in testcases:
15231525
with self.subTest(fraction=fraction, spec=spec):
@@ -1616,12 +1618,6 @@ def test_invalid_formats(self):
16161618
'=010%',
16171619
'>00.2f',
16181620
'>00f',
1619-
# Too many zeros - minimum width should not have leading zeros
1620-
'006f',
1621-
# Leading zeros in precision
1622-
'.010f',
1623-
'.02f',
1624-
'.000f',
16251621
# Missing precision
16261622
'.e',
16271623
'.f',
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Accept leading zeros in precision and width fields for
2+
:class:`~fractions.Fraction` formatting, for example ``format(Fraction(1,
3+
3), '.016f')``.

0 commit comments

Comments
 (0)