GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-130662: accept leading zeros in precision/width for Fraction's formatting by skirpichev · Pull Request #130663 · python/cpython

Conversation

@skirpichev

@skirpichev skirpichev commented Feb 28, 2025 •
edited by bedevere-app Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member
  • Issue: format(Fraction(1, 3), '.016f') raises ValueError #130662

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@skirpichev
pythongh-130662: make Fraction's formatting more compatible wrt float's
caefd01
@skirpichev skirpichev added the skip news label Feb 28, 2025
@bedevere-app bedevere-app Bot added the awaiting review label Feb 28, 2025
@bedevere-app bedevere-app Bot mentioned this pull request Feb 28, 2025
format(Fraction(1, 3), '.016f') raises ValueError #130662
Closed
@skirpichev

skirpichev commented Feb 28, 2025

Copy link
Copy Markdown
Member Author

CC @ericvsmith per experts index

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

serhiy-storchaka
serhiy-storchaka approved these changes Feb 28, 2025
View reviewed changes

@serhiy-storchaka serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Lib/test/test_fractions.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting review labels Feb 28, 2025
skirpichev
skirpichev commented Feb 28, 2025
View reviewed changes
Comment thread Lib/test/test_fractions.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

gvanrossum
gvanrossum reviewed Feb 28, 2025
View reviewed changes
Comment thread Lib/fractions.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

gvanrossum
gvanrossum reviewed Feb 28, 2025
View reviewed changes

@gvanrossum gvanrossum left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

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/3
f"{x:_>010f}"  # '__3.140000', ignores the zero-pad
x = Fraction(1, 3)
Traceback (most recent call last):
  File "<python-input-4>", line 1, in <module>
    f"{x:_>010f}"
      ^^^^^^^^^^
  File "/Users/guido/cpython/Lib/fractions.py", line 600, in __format__
    raise ValueError(
    ...<2 lines>...
    )
ValueError: Invalid format specifier '_>010f' for object of 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?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@skirpichev
Update Lib/test/test_fractions.py
9425ff2
@skirpichev

skirpichev commented Mar 1, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member Author

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@gvanrossum

gvanrossum commented Mar 1, 2025

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@skirpichev skirpichev mentioned this pull request Mar 1, 2025
gh-130662: refine width/precision specification in format-spec #130717
Closed
@skirpichev

skirpichev commented Mar 1, 2025

Copy link
Copy Markdown
Member Author

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@gvanrossum

gvanrossum commented Mar 1, 2025

Copy link
Copy Markdown
Member

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@skirpichev

skirpichev commented Apr 7, 2025

Copy link
Copy Markdown
Member Author

Alternative pr: #130717

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

vstinner
vstinner approved these changes Apr 15, 2025
View reviewed changes

@vstinner vstinner left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

LGTM. I agree with @serhiy-storchaka: #130662 (comment)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@skirpichev skirpichev changed the title gh-130662: make Fraction's formatting more compatible wrt float's gh-130662: accept leading zeros in precision/width for Fraction's formatting Apr 15, 2025
@skirpichev skirpichev added the 3.14 bugs and security fixes label Apr 28, 2025
@skirpichev skirpichev 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 picnixz June 2, 2025 06:29
picnixz
picnixz reviewed Jun 2, 2025
View reviewed changes
Comment thread Lib/test/test_fractions.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/fractions.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

skirpichev added 2 commits June 2, 2025 14:25
@skirpichev
Merge branch 'master' into fix-fmt-fractions/130662
d68c869
@skirpichev
address review: add news
e0d2526
@skirpichev
skirpichev requested a review from picnixz June 2, 2025 11:39
picnixz
picnixz approved these changes Jun 2, 2025
View reviewed changes
Comment thread Misc/NEWS.d/next/Library/2025-06-02-14-28-30.gh-issue-130662.EIgIR8.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

@skirpichev @picnixz
Apply suggestions from code review
a54c7e5
Co-authored-by: Bénédikt Tran <[email protected]>
@vstinner
vstinner merged commit 5bc2d99 into python:main Jun 2, 2025
@bedevere-app bedevere-app Bot removed the awaiting merge label Jun 2, 2025
@miss-islington-app

miss-islington-app Bot commented Jun 2, 2025

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.
🐍🍒⛏🤖

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jun 2, 2025
@skirpichev @picnixz @miss-islington
pythongh-130662: Accept leading zeros in precision/width for Fraction…
2df70e3
…'s formatting (pythonGH-130663)

(cherry picked from commit 5bc2d99)

Co-authored-by: Sergey B Kirpichev <[email protected]>
Co-authored-by: Bénédikt Tran <[email protected]>
@bedevere-app

bedevere-app Bot commented Jun 2, 2025

Copy link
Copy Markdown

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

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

vstinner commented Jun 2, 2025

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@picnixz

picnixz commented Jun 2, 2025

Copy link
Copy Markdown
Member

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner

vstinner commented Jun 2, 2025

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.

picnixz reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@skirpichev

skirpichev commented Jun 2, 2025

Copy link
Copy Markdown
Member Author

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

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

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

vstinner commented Jun 2, 2025

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@skirpichev

skirpichev commented Jun 2, 2025

Copy link
Copy Markdown
Member Author

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@serhiy-storchaka

serhiy-storchaka commented Jun 2, 2025

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@skirpichev skirpichev mentioned this pull request Jul 7, 2025
Add underscore as a decimal separator for string formatting #87790
Closed
@serhiy-storchaka serhiy-storchaka added the needs backport to 3.14 bugs and security fixes label Jul 7, 2025
@miss-islington-app

miss-islington-app Bot commented Jul 7, 2025

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.
🐍🍒⛏🤖

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jul 7, 2025
@skirpichev @picnixz @miss-islington
pythongh-130662: Accept leading zeros in precision/width for Fraction…
c53c357
…'s formatting (pythonGH-130663)

(cherry picked from commit 5bc2d99)

Co-authored-by: Sergey B Kirpichev <[email protected]>
Co-authored-by: Bénédikt Tran <[email protected]>
@bedevere-app

bedevere-app Bot commented Jul 7, 2025

Copy link
Copy Markdown

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot 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
@miss-islington @skirpichev @picnixz
[3.14] gh-130662: Accept leading zeros in precision/width for Fractio…
b2a2f03
…n's formatting (GH-130663) (#136361)

Co-authored-by: Sergey B Kirpichev <[email protected]>
Co-authored-by: Bénédikt Tran <[email protected]>
Pranjal095 pushed a commit to Pranjal095/cpython that referenced this pull request Jul 12, 2025
@skirpichev @picnixz @Pranjal095
pythongh-130662: Accept leading zeros in precision/width for Fraction…
a14ca96
…'s formatting (python#130663)

Co-authored-by: Bénédikt Tran <[email protected]>
taegyunkim pushed a commit to taegyunkim/cpython that referenced this pull request Aug 4, 2025
@skirpichev @picnixz @taegyunkim
pythongh-130662: Accept leading zeros in precision/width for Fraction…
1207ea5
…'s formatting (python#130663)

Co-authored-by: Bénédikt Tran <[email protected]>
Agent-Hellboy pushed a commit to Agent-Hellboy/cpython that referenced this pull request Aug 19, 2025
@skirpichev @picnixz @Agent-Hellboy
pythongh-130662: Accept leading zeros in precision/width for Fraction…
0624b1e
…'s formatting (python#130663)

Co-authored-by: Bénédikt Tran <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@gvanrossum gvanrossum gvanrossum left review comments
@serhiy-storchaka serhiy-storchaka serhiy-storchaka approved these changes
@vstinner vstinner vstinner approved these changes
@picnixz picnixz picnixz approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

skip news

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

5 participants

@skirpichev @gvanrossum @vstinner @picnixz @serhiy-storchaka
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.