From caefd01d0397b1171ebad3ecca600ab5da5ac06e Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Fri, 28 Feb 2025 05:07:49 +0300 Subject: [PATCH 1/4] gh-130662: make Fraction's formatting more compatible wrt float's --- Lib/fractions.py | 4 ++-- Lib/test/test_fractions.py | 8 ++------ 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/Lib/fractions.py b/Lib/fractions.py index f0cbc8c2e6c012..d25935f7602842 100644 --- a/Lib/fractions.py +++ b/Lib/fractions.py @@ -168,9 +168,9 @@ def _round_to_figures(n, d, figures): # A '0' that's *not* followed by another digit is parsed as a minimum width # rather than a zeropad flag. (?P0(?=[0-9]))? - (?P0|[1-9][0-9]*)? + (?P[0-9]+)? (?P[,_])? - (?:\.(?P0|[1-9][0-9]*))? + (?:\.(?P[0-9]+))? (?P[eEfFgG%]) """, re.DOTALL | re.VERBOSE).fullmatch diff --git a/Lib/test/test_fractions.py b/Lib/test/test_fractions.py index 98dccbec9566ac..e3a8f7c8d61aac 100644 --- a/Lib/test/test_fractions.py +++ b/Lib/test/test_fractions.py @@ -1530,6 +1530,8 @@ def test_format_f_presentation_type(self): (F(51, 1000), '.1f', '0.1'), (F(149, 1000), '.1f', '0.1'), (F(151, 1000), '.1f', '0.2'), + (F(1, 3), '.016f', '0.3333333333333333'), # issue gh-130662 + (F(1, 3), '0030.016f', '0000000000000.3333333333333333'), ] for fraction, spec, expected in testcases: with self.subTest(fraction=fraction, spec=spec): @@ -1628,12 +1630,6 @@ def test_invalid_formats(self): '=010%', '>00.2f', '>00f', - # Too many zeros - minimum width should not have leading zeros - '006f', - # Leading zeros in precision - '.010f', - '.02f', - '.000f', # Missing precision '.e', '.f', From 9425ff2e8381241b092f037349b37d630e837721 Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Sat, 1 Mar 2025 04:04:20 +0300 Subject: [PATCH 2/4] Update Lib/test/test_fractions.py --- Lib/test/test_fractions.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_fractions.py b/Lib/test/test_fractions.py index e3a8f7c8d61aac..6a0ec74750ef13 100644 --- a/Lib/test/test_fractions.py +++ b/Lib/test/test_fractions.py @@ -1530,8 +1530,8 @@ def test_format_f_presentation_type(self): (F(51, 1000), '.1f', '0.1'), (F(149, 1000), '.1f', '0.1'), (F(151, 1000), '.1f', '0.2'), - (F(1, 3), '.016f', '0.3333333333333333'), # issue gh-130662 - (F(1, 3), '0030.016f', '0000000000000.3333333333333333'), + (F(22, 7), '.02f', '3.14'), # issue gh-130662 + (F(22, 7), '005.02f', '03.14'), ] for fraction, spec, expected in testcases: with self.subTest(fraction=fraction, spec=spec): From e0d2526a1ba8a16841e917b29094a8dd719b5b3d Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Mon, 2 Jun 2025 14:28:40 +0300 Subject: [PATCH 3/4] address review: add news --- .../Library/2025-06-02-14-28-30.gh-issue-130662.EIgIR8.rst | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2025-06-02-14-28-30.gh-issue-130662.EIgIR8.rst diff --git a/Misc/NEWS.d/next/Library/2025-06-02-14-28-30.gh-issue-130662.EIgIR8.rst b/Misc/NEWS.d/next/Library/2025-06-02-14-28-30.gh-issue-130662.EIgIR8.rst new file mode 100644 index 00000000000000..9d541eef7d647a --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-06-02-14-28-30.gh-issue-130662.EIgIR8.rst @@ -0,0 +1,3 @@ +Accept leading zeros in precision/width fields for +:class:`~fractions.Fraction` formatting, for example ``format(Fraction(1, +3), '.016f')``. From a54c7e57c5dbc411b27d2f2ca21a062e3fc49f43 Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Mon, 2 Jun 2025 14:55:03 +0300 Subject: [PATCH 4/4] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> --- .../next/Library/2025-06-02-14-28-30.gh-issue-130662.EIgIR8.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2025-06-02-14-28-30.gh-issue-130662.EIgIR8.rst b/Misc/NEWS.d/next/Library/2025-06-02-14-28-30.gh-issue-130662.EIgIR8.rst index 9d541eef7d647a..e07200f9a3fbbd 100644 --- a/Misc/NEWS.d/next/Library/2025-06-02-14-28-30.gh-issue-130662.EIgIR8.rst +++ b/Misc/NEWS.d/next/Library/2025-06-02-14-28-30.gh-issue-130662.EIgIR8.rst @@ -1,3 +1,3 @@ -Accept leading zeros in precision/width fields for +Accept leading zeros in precision and width fields for :class:`~fractions.Fraction` formatting, for example ``format(Fraction(1, 3), '.016f')``.