-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
Conversion from fractions.Fraction to bool #83455
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
Comments
As of now, fractions.Fraction.bool is implemented as: This message follows a thread here: numpy/numpy#15277 . |
Agreed that bool should return an actual bool. |
For completeness and to save people going to the NumPy tracker, here's an example of the problem: Python 3.8.1 (default, Jan 5 2020, 21:32:35)
[Clang 10.0.1 (clang-1001.0.46.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import fractions, numpy
>>> f = fractions.Fraction(numpy.int64(1), 2)
>>> not f
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: __bool__ should return bool, returned numpy.bool_ |
Yes, it's a good fix. It's used on other places. -- numbers.Complex.__bool__() also uses "self != 0". Is it an issue? |
Thanks for the quick responses. @victor Stinner, I suppose you could change But I am not sure I feel it is necessary. NumPy is a bit a strange in that it uses its own boolean scalar, and it is easy to override the slot for such objects. |
Does someone want to propose a PR to modify fractions.Fraction.__bool__() to use "return bool(a._numerator)"? |
Hi all, I think this issue can be closed right? |
"Successful" PR would be merged into master. This issue is still in "patch review" stage. |
Thanks Sebastian Berg for the fix, and thanks François Durand for the bug report! This issue is fixed in the master branch, and backports to 3.7 and 3.8 will land soon automatically (once the CI tests pass). |
Fraction.__bool__
returns a bool #18017Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: