[Python-checkins] bpo-36546: More tests: type preservation and equal inputs (#13000)

Raymond Hettinger webhook-mailer at python.org
Mon Apr 29 00:32:05 EDT 2019


https://github.com/python/cpython/commit/db81ba1393af40ba920a996651e2c11943c3663c
commit: db81ba1393af40ba920a996651e2c11943c3663c
branch: master
author: Raymond Hettinger <rhettinger at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2019-04-28T21:31:55-07:00
summary:

bpo-36546: More tests: type preservation and equal inputs (#13000)

files:
M Lib/test/test_statistics.py

diff --git a/Lib/test/test_statistics.py b/Lib/test/test_statistics.py
index c988d7fd8be7..0a967055f0fe 100644
--- a/Lib/test/test_statistics.py
+++ b/Lib/test/test_statistics.py
@@ -2153,12 +2153,11 @@ def test_specific_cases(self):
                 ]:
             self.assertEqual(expected, quantiles(data, n=n))
             self.assertEqual(len(quantiles(data, n=n)), n - 1)
-            self.assertEqual(list(map(float, expected)),
-                             quantiles(map(Decimal, data), n=n))
-            self.assertEqual(list(map(Decimal, expected)),
-                             quantiles(map(Decimal, data), n=n))
-            self.assertEqual(list(map(Fraction, expected)),
-                             quantiles(map(Fraction, data), n=n))
+            # Preserve datatype when possible
+            for datatype in (float, Decimal, Fraction):
+                result = quantiles(map(datatype, data), n=n)
+                self.assertTrue(all(type(x) == datatype) for x in result)
+                self.assertEqual(result, list(map(datatype, expected)))
             # Invariant under tranlation and scaling
             def f(x):
                 return 3.5 * x - 1234.675
@@ -2199,12 +2198,11 @@ def test_specific_cases_inclusive(self):
                 ]:
             self.assertEqual(expected, quantiles(data, n=n, method="inclusive"))
             self.assertEqual(len(quantiles(data, n=n, method="inclusive")), n - 1)
-            self.assertEqual(list(map(float, expected)),
-                             quantiles(map(Decimal, data), n=n, method="inclusive"))
-            self.assertEqual(list(map(Decimal, expected)),
-                             quantiles(map(Decimal, data), n=n, method="inclusive"))
-            self.assertEqual(list(map(Fraction, expected)),
-                             quantiles(map(Fraction, data), n=n, method="inclusive"))
+            # Preserve datatype when possible
+            for datatype in (float, Decimal, Fraction):
+                result = quantiles(map(datatype, data), n=n, method="inclusive")
+                self.assertTrue(all(type(x) == datatype) for x in result)
+                self.assertEqual(result, list(map(datatype, expected)))
             # Invariant under tranlation and scaling
             def f(x):
                 return 3.5 * x - 1234.675
@@ -2222,6 +2220,14 @@ def f(x):
             self.assertTrue(all(math.isclose(e, a, abs_tol=0.0001)
                             for e, a in zip(expected, actual)))
 
+    def test_equal_inputs(self):
+        quantiles = statistics.quantiles
+        for n in range(2, 10):
+            data = [10.0] * n
+            self.assertEqual(quantiles(data), [10.0, 10.0, 10.0])
+            self.assertEqual(quantiles(data, method='inclusive'),
+                            [10.0, 10.0, 10.0])
+
     def test_equal_sized_groups(self):
         quantiles = statistics.quantiles
         total = 10_000



More information about the Python-checkins mailing list