Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-130662: make Fraction's formatting more compatible wrt float's #130663

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

skirpichev
Copy link
Member

@skirpichev skirpichev commented Feb 28, 2025

@skirpichev
Copy link
Member Author

CC @ericvsmith per experts index

Copy link
Member

@serhiy-storchaka serhiy-storchaka left a 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.

Copy link
Member

@gvanrossum gvanrossum left a 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?

@skirpichev
Copy link
Member Author

skirpichev commented Mar 1, 2025

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
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
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.

@gvanrossum
Copy link
Member

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

@skirpichev
Copy link
Member Author

Alternative pr: #130717

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants