-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
gh-119594: Improve pow(fraction.Fraction(), b, modulo) error message #119593
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
Conversation
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.
Fraction.__pow__
should act same as float.__pow__
. This new code does not. pow(3.0, 4, 5) and
pow(3.5, 4, 5)` raise "TypeError: pow() 3rd argument not allowed unless all arguments are integers" Put following at top of code:
if mod is not None:
raise TypeError("pow() 3rd argument not allowed unless all arguments are integers")
and correct the new test.
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 And if you don't make the requested changes, you will be poked with soft cushions! |
Slight modification to Terry's suggestion: I think we can do: if mod is not None:
return NotImplemented and then the general machinery should construct a reasonable error message for us. And in theory this gives other classes a chance to participate where |
I have made the requested changes; please review again. Should edit: Found a sort-of answer in the docs, where it says
The ternary coercion rules in PEP 208 do seem complicated. So according to the status quo, seems a third argument for |
Thanks for making the requested changes! @terryjreedy: please review the changes made to this pull request. |
Agreed, I think. I don't think it would be actively wrong to add the check to |
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.
Should not __rpow__
be modified too?
The conclusion that @wimglenn came to in the comments above (and that I think I agree with) is that |
Ah, sorry, I did not read the discussion before looking at the code in this PR. I experimented and was not able to trigger calling |
Yep, same here. |
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!
@terryjreedy Okay to merge?
I am changing title and blurb to reflect user visible effect of change. Will then merge. |
Misc/NEWS.d/next/Library/2024-05-26-22-22-51.gh-issue-119594.fnQNM8.rst
Outdated
Show resolved
Hide resolved
…ssage (python#119593) If one calls pow(fractions.Fraction, x, module) with modulo not None, the error message now says that the types are incompatible rather than saying pow only takes 2 arguments. Implemented by having fractions.Fraction __pow__ accept optional modulo argument and return NotImplemented if not None. pow() then raises with appropriate message. --------- Co-authored-by: Mark Dickinson <dickinsm@gmail.com>
…ssage (python#119593) If one calls pow(fractions.Fraction, x, module) with modulo not None, the error message now says that the types are incompatible rather than saying pow only takes 2 arguments. Implemented by having fractions.Fraction __pow__ accept optional modulo argument and return NotImplemented if not None. pow() then raises with appropriate message. --------- Co-authored-by: Mark Dickinson <dickinsm@gmail.com>
Fraction.__pow__
does not accept the third argument whichpow
tries to pass to it:Other number types don't do that, they support it when they can and print an error message if they don't want to, e.g.
I'm not sure what fraction should do, maybe it should just continue not to support it? But accept the argument, and then print a better error message if modulo is not None?
Maybe it is worth adding tests for all the possible coercions too (ref: https://peps.python.org/pep-0208/)