-
-
Notifications
You must be signed in to change notification settings - Fork 30.7k
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-101773: Optimize creation of Fractions in private methods #101780
Conversation
130db33
to
9cc62f7
Compare
Probably, from_float/decimal methods should be adapted to use this. Decimal.as_integer_ratio is in lowest terms. I guess, that same holds for the float method, but that's undocumented, unfortunately. |
It does indeed. It would be reasonable to document it, I think. |
On Sat, Feb 11, 2023 at 04:53:50AM -0800, Mark Dickinson wrote:
I think you can and should assume that all core developers are very
familiar with PEP 8. :-)
Here I do care more about users knowledge.
The single leading underscore as a stigmate of private (internaly used)
entity is more or less common convention across the python ecosystem.
Sometimes it's enforced (e.g. help() hides such methods, this stuff is
not included in star-imports and so on). I wonder if we shouldn't do same
with _-prefixed kwargs (at least in help()).
That's a rather dogmatic approach. On CPython, we should prefer a more
pragmatic approach: if we have reasonable grounds to believe that a change
might break user code then we should take reasonable steps to avoid that
breakage. It's not friendly to knowingly break existing code and then tell
people "well, it was your fault for using something undocumented".
But the Python is a dynamical language and with great introspection
capabilities... In this way we can end with supporting everything
as part of the API, because people actually *can* use everything.
|
@mdickinson, |
Indeed. :-) As with most aspects of software development, careful judgement is required, evaluating the context and balancing the trade offs within that context. Few things are black and white. The fact that
Hmm, not really. :-) I don't want to leave a
Of those options, option 3 just seems like too much code churn for too little benefit. There was a recent |
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
That is a more generic problem: we have PEP8's convention where _single_leading_underscore is weak “internal use” indicator. On another hand, these arguments are available for introspection and shown in the help() output, IDE tooltips and so on. Probably, all above use the signature() function. Maybe it does make sense to add a
(Probably it was about this thread.) Indeed, there was an impression that all methods do support unnormalized fractions. offtopicOn another hand 1. there is no doubts that the argument was private 2. most methods actually do support this (arithmetics) and 3. much more can if we do a cheap normalization (ensure that denominator is positive)So, I think the proposal might have some sense and it's possible to implement it with a little overhead for canonicalized fractions. E.g. we can mark unnormalized fractions with a sticky flag (or flags) and do special steps to deal with this case in some methods. Not sure it worth to have this in the stdlib: to really support unnormalized fractions almost every method should be rewritten (e.g. arithmetic methods do some normalization of the output, which we probably want to avoid for unnormalized fractions). PS: |
Thanks for making the requested changes! @mdickinson: please review the changes made to this pull request. |
@skirpichev @mdickinson The changes look good to me. With the removal of the (semi-private) argument |
Misc/NEWS.d/next/Library/2023-02-10-11-59-13.gh-issue-101773.J_kI7y.rst
Outdated
Show resolved
Hide resolved
…_kI7y.rst Co-authored-by: Pieter Eendebak <pieter.eendebak@gmail.com>
4339283
to
580b1be
Compare
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 tweaked the docstring wording on _from_coprime_ints
slightly.
I'll merge when CI completes.
Thanks to all reviewers. |
* main: (67 commits) pythongh-99108: Add missing md5/sha1 defines to Modules/Setup (python#102308) pythongh-100227: Move _str_replace_inf to PyInterpreterState (pythongh-102333) pythongh-100227: Move the dtoa State to PyInterpreterState (pythongh-102331) pythonGH-102305: Expand some macros in generated_cases.c.h (python#102309) Migrate to new PSF mailgun account (python#102284) pythongh-102192: Replace PyErr_Fetch/Restore etc by more efficient alternatives (in Python/) (python#102193) pythonGH-90744: Fix erroneous doc links in the sys module (python#101319) pythongh-87092: Make jump target label equal to the offset of the target in the instructions sequence (python#102093) pythongh-101101: Unstable C API tier (PEP 689) (pythonGH-101102) IDLE: Simplify DynOptionsMenu __init__code (python#101371) pythongh-101561: Add typing.override decorator (python#101564) pythongh-101825: Clarify that as_integer_ratio() output is always normalized (python#101843) pythongh-101773: Optimize creation of Fractions in private methods (python#101780) pythongh-102251: Updates to test_imp Toward Fixing Some Refleaks (pythongh-102254) pythongh-102296 Document that inspect.Parameter kinds support ordering (pythonGH-102297) pythongh-102250: Fix double-decref in COMPARE_AND_BRANCH error case (pythonGH-102287) pythongh-101100: Fix sphinx warnings in `types` module (python#102274) pythongh-91038: Change default argument value to `False` instead of `0` (python#31621) pythongh-101765: unicodeobject: use Py_XDECREF correctly (python#102283) [doc] Improve grammar/fix missing word (pythonGH-102060) ...
…numerator/denominator pair is already normalised. Follows python/cpython#101780
https://build.opensuse.org/request/show/1073017 by user dgarcia + dimstar_suse - Enable python 3.11 build again, now is supported - Update to 1.14 - Implement __format__ for Fraction, following python/cpython#100161 - Implement Fraction.is_integer(), following python/cpython#100488 - Fraction.limit_denominator() is faster, following python/cpython#93730 - Internal creation of result Fractions is about 10% faster if the calculated numerator/denominator pair is already normalised, following python/cpython#101780 - Built using Cython 3.0.0b1. - 1.13 - Parsing very long numbers from a fraction string was very slow, even slower than fractions.Fraction. The parser is now faster in all cases (and still much faster for shorter numbers). - Fraction did not implement __int__. https://bugs.python.org/issue44547 - 1.12 - Faster and more spa
_normalize
argument of the Fraction constructor