]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-45852: Fix the Counter/iter test for statistics.mode() (GH-29667)
authorRaymond Hettinger <rhettinger@users.noreply.github.com>
Sat, 20 Nov 2021 17:01:09 +0000 (11:01 -0600)
committerGitHub <noreply@github.com>
Sat, 20 Nov 2021 17:01:09 +0000 (11:01 -0600)
Suggested by Stefan Pochmann.

Lib/test/test_statistics.py

index 5cb055c981c448bac51f24b7562d612f024e92f9..fbc6a071cfd34b652adc5642ceb199de31ab46aa 100644 (file)
@@ -1900,10 +1900,13 @@ class TestMode(NumericTestCase, AverageMixin, UnivariateTypeMixin):
 
     def test_counter_data(self):
         # Test that a Counter is treated like any other iterable.
-        data = collections.Counter([1, 1, 1, 2])
-        # Since the keys of the counter are treated as data points, not the
-        # counts, this should return the first mode encountered, 1
-        self.assertEqual(self.func(data), 1)
+        # We're making sure mode() first calls iter() on its input.
+        # The concern is that a Counter of a Counter returns the original
+        # unchanged rather than counting its keys.
+        c = collections.Counter(a=1, b=2)
+        # If iter() is called, mode(c) loops over the keys, ['a', 'b'],
+        # all the counts will be 1, and the first encountered mode is 'a'.
+        self.assertEqual(self.func(c), 'a')
 
 
 class TestMultiMode(unittest.TestCase):