Skip to content

Commit b2a2f03

Browse files
miss-islingtonskirpichevpicnixz
authored
[3.14] gh-130662: Accept leading zeros in precision/width for Fraction's formatting (GH-130663) (#136361)
Co-authored-by: Sergey B Kirpichev <[email protected]> Co-authored-by: Bénédikt Tran <[email protected]>
1 parent 57d6db5 commit b2a2f03

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
@@ -1515,6 +1515,8 @@ def test_format_f_presentation_type(self):
15151515
(F(51, 1000), '.1f', '0.1'),
15161516
(F(149, 1000), '.1f', '0.1'),
15171517
(F(151, 1000), '.1f', '0.2'),
1518+
(F(22, 7), '.02f', '3.14'), # issue gh-130662
1519+
(F(22, 7), '005.02f', '03.14'),
15181520
]
15191521
for fraction, spec, expected in testcases:
15201522
with self.subTest(fraction=fraction, spec=spec):
@@ -1613,12 +1615,6 @@ def test_invalid_formats(self):
16131615
'=010%',
16141616
'>00.2f',
16151617
'>00f',
1616-
# Too many zeros - minimum width should not have leading zeros
1617-
'006f',
1618-
# Leading zeros in precision
1619-
'.010f',
1620-
'.02f',
1621-
'.000f',
16221618
# Missing precision
16231619
'.e',
16241620
'.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)