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-67790: Support float-style formatting for Fraction instances #100161
base: main
Are you sure you want to change the base?
Conversation
|
Name | Link |
---|---|
983726f | |
https://app.netlify.com/sites/python-cpython-preview/deploys/6394be329cefee00083eddef |
Converting to draft while I pacify the doc build. |
Doc build duly pacified; ready for review. |
… 'e' presentation type
@ericvsmith Would you be willing to review at some point? I'm not looking for detailed line-by-line review (though that would be useful too) so much as big-picture "is this a good idea?" review. In particular, I want to avoid doing anything here that will be hard to undo later if it conflicts with a different approach that we want to take, and that's why I restricted to just implementing the |
Hi, @mdickinson. Yes, I'll take a look. I'm going to be out of town for a few days, but will review when I get back. |
@ericvsmith Thanks! No urgency - I'd quite like to get this in for 3.12, but we have a good few weeks (months, even!) before feature freeze. |
Can we also test the examples from the related issues:
|
@jowagner Yes, I can definitely add those. |
This PR adds support for float-style formatting for
Fraction
objects: it supports the"e"
,"E"
,"f"
,"F"
,"g"
,"G"
and"%"
presentation types, and all the various bells and whistles of the formatting mini-language for those presentation types. The behaviour almost exactly matches that offloat
, but the implementation works with the exactFraction
value and does not do an intermediate conversion tofloat
, and so avoids loss of precision or issues with numbers that are outside the dynamic range of thefloat
type.Note that the
"n"
presentation type is not supported. That support could be added later if people have a need for it.There's one corner-case where the behaviour differs from that of float: for the
float
type, if explicit alignment is specified with a fill character of'0'
and alignment type'='
, then thousands separators (if specified) are inserted into the padding string:The exact same effect can be achieved by using the
'0'
flag:For
Fraction
, only the'0'
flag has the above behaviour with respect to thousands separators: there's no special-casing of the particular'0='
fill-character/alignment combination. Instead, we treat the fill character'0'
just like any other:The
Fraction
formatter is also stricter about combining these two things: it's not permitted to use both the'0'
flag and explicit alignment, on the basis that we should refuse the temptation to guess in the face of ambiguity.float
is less picky: