diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2020-02-06 15:13:38 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-02-06 15:13:38 (GMT) |
commit | 0d03a1028200646479ef9bb0ad8973d0e73f9525 (patch) | |
tree | 98bdca24fac5745d2f0802d37499d35769cc9005 /Lib/test | |
parent | 708f472dd92f4f46c27ace710492da65da4a3319 (diff) | |
download | cpython-0d03a1028200646479ef9bb0ad8973d0e73f9525.zip cpython-0d03a1028200646479ef9bb0ad8973d0e73f9525.tar.gz cpython-0d03a1028200646479ef9bb0ad8973d0e73f9525.tar.bz2 |
bpo-39274: Ensure Fraction.__bool__() returns a bool (GH-18017)
Some numerator types used (specifically NumPy) decides to not
return a Python boolean for the "a != b" operation. Using the equivalent
call to bool() guarantees a bool return also for such types.
(cherry picked from commit 427c84f13f7719e6014a21bd1b81efdc02a046fb)
Co-authored-by: Sebastian Berg <sebastian@sipsolutions.net>
Diffstat (limited to 'Lib/test')
-rw-r--r-- | Lib/test/test_fractions.py | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/Lib/test/test_fractions.py b/Lib/test/test_fractions.py index 18ab28c..86b49f3 100644 --- a/Lib/test/test_fractions.py +++ b/Lib/test/test_fractions.py @@ -6,6 +6,7 @@ import math import numbers import operator import fractions +import functools import sys import unittest import warnings @@ -346,6 +347,42 @@ class FractionTest(unittest.TestCase): self.assertTypedEquals(0.1+0j, complex(F(1,10))) + def testBoolGuarateesBoolReturn(self): + # Ensure that __bool__ is used on numerator which guarantees a bool + # return. See also bpo-39274. + @functools.total_ordering + class CustomValue: + denominator = 1 + + def __init__(self, value): + self.value = value + + def __bool__(self): + return bool(self.value) + + @property + def numerator(self): + # required to preserve `self` during instantiation + return self + + def __eq__(self, other): + raise AssertionError("Avoid comparisons in Fraction.__bool__") + + __lt__ = __eq__ + + # We did not implement all abstract methods, so register: + numbers.Rational.register(CustomValue) + + numerator = CustomValue(1) + r = F(numerator) + # ensure the numerator was not lost during instantiation: + self.assertIs(r.numerator, numerator) + self.assertIs(bool(r), True) + + numerator = CustomValue(0) + r = F(numerator) + self.assertIs(bool(r), False) + def testRound(self): self.assertTypedEquals(F(-200), round(F(-150), -2)) self.assertTypedEquals(F(-200), round(F(-250), -2)) |