-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
gh-67790: Support basic formatting for Fraction #111320
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 basic formatting for Fraction #111320
Conversation
Fraction.__format__
@serhiy-storchaka @ericvsmith Would either of you be interested in reviewing? |
|
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.
It is perhaps out of the scope of this PR or this issue, but it may be useful to provide options for:
- output as improper fraction: 3/2 as
1 1/2
. always output a fraction slash: 3/1 as(already possible with the3/1
.#
modifier)- specify different symbol for fraction slash, e.g.
:
,÷
(U+00F7),⁄
(U+2044),∕
(U+2215),∶
(U+2236).
@serhiy-storchaka Thank you for reviewing!
Hmm, possibly. Now that I look at it, we're adding two ways to do exactly the same thing (that is, What do you think about simply dropping the
Yes, I think I'd rather leave these to a separate PR. |
Looks fine to me. |
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
Actually, I think I'd require the "d", just in case we want to do somethings else entirely without it. But I don't feel strongly about it. OTOH, note that for ints, the 'd' has no effect. |
@mdickinson It LGTM if drop 'd' and "integer-like". |
Thanks @serhiy-storchaka and @ericvsmith for feedback.
Yes, I can see a case both ways, and part of me wants to require a presentation type for all non-trivial formatting. I think this is close enough to the obvious one way to do formatting for Fractions that I'm happy for it to be the "default" formatting, so not require a "d". I'll make that change. |
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. Thanks, Mark!
https://build.opensuse.org/request/show/1140281 by user dirkmueller + anag+factory - update to 1.16: * Formatting support was improved, following CPython 3.13a3 as of python/cpython#111320 * Add support for Python 3.13 by using Cython 3.0.8 and calling math.gcd().
https://build.opensuse.org/request/show/1140281 by user dirkmueller + anag+factory - update to 1.16: * Formatting support was improved, following CPython 3.13a3 as of python/cpython#111320 * Add support for Python 3.13 by using Cython 3.0.8 and calling math.gcd().
https://build.opensuse.org/request/show/1140281 by user dirkmueller + anag+factory - update to 1.16: * Formatting support was improved, following CPython 3.13a3 as of python/cpython#111320 * Add support for Python 3.13 by using Cython 3.0.8 and calling math.gcd().
PR python#100161 added fancy float-style formatting for the Fraction type, but left us in a state where basic formatting for fractions (alignment, fill, minimum width, thousands separators) still wasn't supported. This PR adds that support. --------- Co-authored-by: Serhiy Storchaka <[email protected]>
PR python#100161 added fancy float-style formatting for the Fraction type, but left us in a state where basic formatting for fractions (alignment, fill, minimum width, thousands separators) still wasn't supported. This PR adds that support. --------- Co-authored-by: Serhiy Storchaka <[email protected]>
PR #100161 added fancy float-style formatting for the
Fraction
type, but left us in a state where basic formatting for fractions (alignment, fill, thousands separators) still wasn't supported. For example, we can't currently specify a minimum width for a formatted fraction:This PR adds that basic formatting support, aiming for compatibility with
int
formatting. The basic formatting is active either with presentation typed
, or with no explicit presentation type. For example, on this branch:All of the above except
f"{y}"
currently giveValueError
on main.Some details:
0
is not supported (though it's still fine to use0
as a fill character in the normal way): it's not 100% clear what the semantics should be (should both the numerator and denominator be padded? Just the numerator?) or what value it would provide, so until someone comes up with valid use-cases that can help inform the semantics, I'm leaving this unsupported.#
forces a slash and denominator in the formatted output, even when the output is integer-valued. By default, the slash and denominator are left off for integer-valued fractions.Pinging @ericvsmith for awareness.
📚 Documentation preview 📚: https://cpython-previews--111320.org.readthedocs.build/