diff options
author | Nick Coghlan <ncoghlan@gmail.com> | 2014-02-08 09:58:04 (GMT) |
---|---|---|
committer | Nick Coghlan <ncoghlan@gmail.com> | 2014-02-08 09:58:04 (GMT) |
commit | 73afe2a972e30a3e0f87401be2fa38c67e2cb964 (patch) | |
tree | bf900c405b1584f00248558dad103050d5a31aaa /Lib/test/test_statistics.py | |
parent | 57e41277813163a90129a8c4d5f62f0ca68793b6 (diff) | |
download | cpython-73afe2a972e30a3e0f87401be2fa38c67e2cb964.zip cpython-73afe2a972e30a3e0f87401be2fa38c67e2cb964.tar.gz cpython-73afe2a972e30a3e0f87401be2fa38c67e2cb964.tar.bz2 |
Close #20481: Disallow mixed type input in statistics
The most appropriate coercion rules are not yet clear, so simply
disallowing mixed type input for 3.4.
(Committed on Steven's behalf)
Diffstat (limited to 'Lib/test/test_statistics.py')
-rw-r--r-- | Lib/test/test_statistics.py | 59 |
1 files changed, 25 insertions, 34 deletions
diff --git a/Lib/test/test_statistics.py b/Lib/test/test_statistics.py index 6db821f..49b8597 100644 --- a/Lib/test/test_statistics.py +++ b/Lib/test/test_statistics.py @@ -687,6 +687,26 @@ class DecimalToRatioTest(unittest.TestCase): self.assertRaises(ValueError, statistics._decimal_to_ratio, d) +class CheckTypeTest(unittest.TestCase): + # Test _check_type private function. + + def test_allowed(self): + # Test that a type which should be allowed is allowed. + allowed = set([int, float]) + statistics._check_type(int, allowed) + statistics._check_type(float, allowed) + + def test_not_allowed(self): + # Test that a type which should not be allowed raises. + allowed = set([int, float]) + self.assertRaises(TypeError, statistics._check_type, Decimal, allowed) + + def test_add_to_allowed(self): + # Test that a second type will be added to the allowed set. + allowed = set([int]) + statistics._check_type(float, allowed) + self.assertEqual(allowed, set([int, float])) + # === Tests for public functions === @@ -881,40 +901,11 @@ class TestSum(NumericTestCase, UnivariateCommonMixin, UnivariateTypeMixin): self.assertRaises(TypeError, self.func, [1, 2, 3, b'999']) def test_mixed_sum(self): - # Mixed sums are allowed. - - # Careful here: order matters. Can't mix Fraction and Decimal directly, - # only after they're converted to float. - data = [1, 2, Fraction(1, 2), 3.0, Decimal("0.25")] - self.assertEqual(self.func(data), 6.75) - - -class SumInternalsTest(NumericTestCase): - # Test internals of the sum function. - - def test_ignore_instance_float_method(self): - # Test that __float__ methods on data instances are ignored. - - # Python typically calls __dunder__ methods on the class, not the - # instance. The ``sum`` implementation calls __float__ directly. To - # better match the behaviour of Python, we call it only on the class, - # not the instance. This test will fail if somebody "fixes" that code. - - # Create a fake __float__ method. - def __float__(self): - raise AssertionError('test fails') - - # Inject it into an instance. - class MyNumber(Fraction): - pass - x = MyNumber(3) - x.__float__ = types.MethodType(__float__, x) - - # Check it works as expected. - self.assertRaises(AssertionError, x.__float__) - self.assertEqual(float(x), 3.0) - # And now test the function. - self.assertEqual(statistics._sum([1.0, 2.0, x, 4.0]), 10.0) + # Mixed input types are not (currently) allowed. + # Check that mixed data types fail. + self.assertRaises(TypeError, self.func, [1, 2.0, Fraction(1, 2)]) + # And so does mixed start argument. + self.assertRaises(TypeError, self.func, [1, 2.0], Decimal(1)) class SumTortureTest(NumericTestCase): |