Skip to content

gh-130662: accept leading zeros in precision/width for Fraction's formatting - #130663

Merged
vstinner merged 5 commits into
python:mainfrom
skirpichev:fix-fmt-fractions/130662
Jun 2, 2025
Merged

gh-130662: accept leading zeros in precision/width for Fraction's formatting#130663
vstinner merged 5 commits into
python:mainfrom
skirpichev:fix-fmt-fractions/130662

Conversation

@skirpichev

@skirpichevskirpichev commented Feb 28, 2025

Copy link
Copy Markdown
Member

@skirpichev

Copy link
Copy Markdown
MemberAuthor

CC @ericvsmith per experts index

@serhiy-storchakaserhiy-storchaka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

I have some minor suggestions for tests.

Comment threadLib/test/test_fractions.py Outdated
Comment threadLib/test/test_fractions.py Outdated
Comment threadLib/fractions.py

@gvanrossumgvanrossum left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that there's another inconsistency between float and Fraction.

x=1/3f"{x:_>010f}"# '__3.140000', ignores the zero-padx=Fraction(1, 3)
Traceback (mostrecentcalllast):
File"<python-input-4>", line1, in<module>f"{x:_>010f}"^^^^^^^^^^File"/Users/guido/cpython/Lib/fractions.py", line600, in__format__raiseValueError(
...<2lines>...
)
ValueError: Invalidformatspecifier'_>010f'forobjectof type 'Fraction'

So we still fail the goal of Fraction supporting everything that float does. Since Fraction actually is more strict (which is the way of the future) aren't we fixing this the wrong way?

@skirpichev

skirpichev commented Mar 1, 2025

Copy link
Copy Markdown
MemberAuthor

Here is implementation pr, for context: #100161

The ValueError raised in case both alignment (and the fill character) and zero padding are specified on the ground "refuse the temptation to guess". In principle, it's not hard to reproduce float's behavior (zero padding is ignored if the fill character is specified). Though, maybe it's better to align this with the Fraction instead. Probably, it's a separate issue. Edit: #130716

Few another incompatibilities: #130664

@gvanrossum

Copy link
Copy Markdown
Member

As happens occasionally, the docs are out of sync with the implementation, and we should respond by updating the docs, not the code. IMO.

@skirpichev

Copy link
Copy Markdown
MemberAuthor

The problem is that the stdlib has several beasts (Decimal and Fraction), that have slightly different implementations of new-style formatting. IMO, documenting all these differences will make documentation much less readable.

Also, it's less obvious to which convention should follow external modules, like the mpmath or the gmpy2.

@gvanrossum

Copy link
Copy Markdown
Member

Okay, I will leave it to more active core devs to sort out.

@skirpichev

Copy link
Copy Markdown
MemberAuthor

Alternative pr: #130717

@vstinnervstinner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@skirpichevskirpichev changed the title gh-130662: make Fraction's formatting more compatible wrt float'sgh-130662: accept leading zeros in precision/width for Fraction's formattingApr 15, 2025
@skirpichevskirpichev added the 3.14 bugs and security fixes label Apr 28, 2025
@skirpichevskirpichev added needs backport to 3.14 bugs and security fixes and removed 3.14 bugs and security fixes labels May 8, 2025
@skirpichev
skirpichev requested a review from picnixzJune 2, 2025 06:29
Comment threadLib/test/test_fractions.py
Comment threadLib/fractions.py
@skirpichev
skirpichev requested a review from picnixzJune 2, 2025 11:39
Comment threadMisc/NEWS.d/next/Library/2025-06-02-14-28-30.gh-issue-130662.EIgIR8.rst Outdated
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
@vstinner
vstinner merged commit 5bc2d99 into python:mainJun 2, 2025
@miss-islington-app

Copy link
Copy Markdown

Thanks @skirpichev for the PR, and @vstinner for merging it 🌮🎉.. I'm working now to backport this PR to: 3.14.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jun 2, 2025
…'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>
@bedevere-app

Copy link
Copy Markdown

GH-135026 is a backport of this pull request to the 3.14 branch.

@bedevere-appbedevere-appBot removed the needs backport to 3.14 bugs and security fixes label Jun 2, 2025
@vstinner

Copy link
Copy Markdown
Member

Merged, thank you.

GH-135026 is a backport of this pull request to the 3.14 branch.

I disagree: we should not backport this change to stable branches.

@picnixz

Copy link
Copy Markdown
Member

Wasn't it a fix for something we introduced in 3.14? @skirpichev

@vstinner

Copy link
Copy Markdown
Member

Wasn't it a fix for something we introduced in 3.14? @skirpichev

No, Python 3.13 has the same behavior for example. It was "always" like that.

@skirpichev proposed to have the behavior for all types: float, Fraction, Decimal. Before, only float accepted leading zeros.

@skirpichev

Copy link
Copy Markdown
MemberAuthor

I disagree: we should not backport this change to stable branches.

Why not? It's a bug, isn't?

@skirpichev
skirpichev deleted the fix-fmt-fractions/130662 branch June 2, 2025 13:56
@vstinner

Copy link
Copy Markdown
Member

Why not? It's a bug, isn't?

From my point of view, it's a new feature and existing projects may rely on the current (Python 3.14) behavior.

@skirpichev

Copy link
Copy Markdown
MemberAuthor

From my point of view, it's a new feature and existing projects may rely on the current (Python 3.14) behavior.

How?!

The issue is about formatting stdlib types (Fractions and Decimals, in a separate pr). Should external projects use current formatting specification mini-language or use more strict rules? This was coming from mpmath/mpmath#915.

@serhiy-storchaka

Copy link
Copy Markdown
Member

From my point of view, this is a borderline between a minor feature and an unsignificant bug fix which we may not want to backport. So maybe to 3.14 only? The solved problem is rather scholastic.

@miss-islington-app

Copy link
Copy Markdown

Thanks @skirpichev for the PR, and @vstinner for merging it 🌮🎉.. I'm working now to backport this PR to: 3.14.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jul 7, 2025
…'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>
@bedevere-app

Copy link
Copy Markdown

GH-136361 is a backport of this pull request to the 3.14 branch.

@bedevere-appbedevere-appBot removed the needs backport to 3.14 bugs and security fixes label Jul 7, 2025
hugovk pushed a commit that referenced this pull request Jul 7, 2025
…n's formatting (GH-130663) (#136361)
Co-authored-by: Sergey B Kirpichev <skirpichev@gmail.com>
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Pranjal095 pushed a commit to Pranjal095/cpython that referenced this pull request Jul 12, 2025
…'s formatting (python#130663)
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
taegyunkim pushed a commit to taegyunkim/cpython that referenced this pull request Aug 4, 2025
…'s formatting (python#130663)
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Agent-Hellboy pushed a commit to Agent-Hellboy/cpython that referenced this pull request Aug 19, 2025
…'s formatting (python#130663)
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants

@skirpichev@gvanrossum@vstinner@picnixz@serhiy-storchaka