This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Conversion from fractions.Fraction to bool
Type: behavior Stage: resolved
Components: Versions: Python 3.9
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Ido Michael, SilentGhost, francois-durand, mark.dickinson, miss-islington, rhettinger, seberg, vstinner
Priority: normal Keywords: newcomer friendly, patch

Created on 2020-01-09 12:43 by francois-durand, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 18017 merged seberg, 2020-01-15 22:47
PR 18376 merged miss-islington, 2020-02-06 14:55
PR 18377 merged miss-islington, 2020-02-06 14:57
Messages (12)
msg359673 - (view) Author: François Durand (francois-durand) Date: 2020-01-09 12:43
As of now, fractions.Fraction.__bool__ is implemented as: ``return a._numerator != 0``. However, this does not necessary return a bool (which would be desired). In particular, when the numerator is a numpy integer, this returns a numpy bool instead. Another solution would be to implement fractions.Fraction.__bool__ as: ``return bool(numerator)``. What do you think?

This message follows a thread here: https://github.com/numpy/numpy/issues/15277 .
msg359675 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2020-01-09 13:14
Agreed that __bool__ should return an actual bool.
``return bool(a._numerator)`` does seem like the obvious way to do this.
msg359677 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2020-01-09 13:25
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_
msg359679 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2020-01-09 13:36
> ``return bool(a._numerator)`` does seem like the obvious way to do this.

Yes, it's a good fix. It's used on other places.

--

numbers.Complex.__bool__() also uses "self != 0". Is it an issue?
msg359681 - (view) Author: Sebastian Berg (seberg) * Date: 2020-01-09 14:02
Thanks for the quick responses.

@Victor Stinner, I suppose you could change `numbers.Complex.__bool__()` by adding the no-op bool to make it: `bool(self != 0)`.

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.
msg360077 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2020-01-15 21:40
Does someone want to propose a PR to modify fractions.Fraction.__bool__() to use "return bool(a._numerator)"?
msg361237 - (view) Author: Ido Michael (Ido Michael) * Date: 2020-02-02 14:31
Hi all,

I think this issue can be closed right?
Saw a successful PR.
msg361238 - (view) Author: SilentGhost (SilentGhost) * (Python triager) Date: 2020-02-02 14:37
"Successful" PR would be merged into master. This issue is still in "patch review" stage.
msg361488 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2020-02-06 14:54
New changeset 427c84f13f7719e6014a21bd1b81efdc02a046fb by Sebastian Berg in branch 'master':
bpo-39274: Ensure Fraction.__bool__() returns a bool (GH-18017)
https://github.com/python/cpython/commit/427c84f13f7719e6014a21bd1b81efdc02a046fb
msg361489 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2020-02-06 14:59
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).
msg361491 - (view) Author: miss-islington (miss-islington) Date: 2020-02-06 15:13
New changeset 0d03a1028200646479ef9bb0ad8973d0e73f9525 by Miss Islington (bot) in branch '3.8':
bpo-39274: Ensure Fraction.__bool__() returns a bool (GH-18017)
https://github.com/python/cpython/commit/0d03a1028200646479ef9bb0ad8973d0e73f9525
msg361492 - (view) Author: miss-islington (miss-islington) Date: 2020-02-06 15:14
New changeset 705d271d553b77fd170d27ab8d0f11f638c7f145 by Miss Islington (bot) in branch '3.7':
bpo-39274: Ensure Fraction.__bool__() returns a bool (GH-18017)
https://github.com/python/cpython/commit/705d271d553b77fd170d27ab8d0f11f638c7f145
History
Date User Action Args
2022-04-11 14:59:25adminsetgithub: 83455
2020-02-06 15:14:42miss-islingtonsetmessages: + msg361492
2020-02-06 15:13:43miss-islingtonsetnosy: + miss-islington
messages: + msg361491
2020-02-06 14:59:07vstinnersetstatus: open -> closed
resolution: fixed
messages: + msg361489

stage: patch review -> resolved
2020-02-06 14:57:06miss-islingtonsetpull_requests: + pull_request17754
2020-02-06 14:55:51miss-islingtonsetpull_requests: + pull_request17753
2020-02-06 14:54:11vstinnersetmessages: + msg361488
2020-02-02 14:37:51SilentGhostsetnosy: + SilentGhost
messages: + msg361238
2020-02-02 14:31:35Ido Michaelsetnosy: + Ido Michael
messages: + msg361237
2020-01-15 22:47:23sebergsetkeywords: + patch
stage: patch review
pull_requests: + pull_request17412
2020-01-15 21:40:05vstinnersetkeywords: + newcomer friendly

messages: + msg360077
versions: - Python 3.5, Python 3.6, Python 3.7, Python 3.8
2020-01-11 01:11:53terry.reedysetmessages: - msg359674
2020-01-09 14:02:46sebergsetnosy: + seberg
messages: + msg359681
2020-01-09 13:36:51vstinnersetnosy: + vstinner
messages: + msg359679
2020-01-09 13:25:31mark.dickinsonsetmessages: + msg359677
2020-01-09 13:14:20mark.dickinsonsetmessages: + msg359675
2020-01-09 13:02:19xtreaksetnosy: + rhettinger, mark.dickinson
2020-01-09 12:45:17francois-durandsetmessages: + msg359674
2020-01-09 12:43:51francois-durandcreate