Skip to content

Commit c53c357

Browse files
skirpichevpicnixz
authored andcommitted
pythongh-130662: Accept leading zeros in precision/width for Fraction's formatting (pythonGH-130663)
(cherry picked from commit 5bc2d99) Co-authored-by: Sergey B Kirpichev <skirpichev@gmail.com> Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
1 parent 8f06a9c commit c53c357

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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy