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: statistics.mode test doesn't test what it claims to
Type: behavior Stage: resolved
Components: Tests Versions: Python 3.11, Python 3.10
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: Stefan Pochmann, miss-islington, rhettinger, steven.daprano
Priority: normal Keywords: patch

Created on 2021-11-20 02:26 by Stefan Pochmann, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 29667 merged rhettinger, 2021-11-20 16:36
PR 29671 merged miss-islington, 2021-11-20 17:01
Messages (4)
msg406642 - (view) Author: Stefan Pochmann (Stefan Pochmann) * Date: 2021-11-20 02:26
This test:

    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)

If the mode() code *were* wrong this way (used Counter(data) instead of Counter(iter(data))), then the test wouldn't detect it, as mode() would still return 1. The test data should be [1, 2, 2, 2] instead, in which case such wrong mode() would return 2.

It used to be correct but wasn't adjusted correctly when mode() switched from raising an error for multiple modes to returning the first. The old code was:

    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 raise.
        self.assertRaises(statistics.StatisticsError, self.func, data)
msg406673 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2021-11-20 16:37
Thanks for noticing this.
msg406674 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2021-11-20 17:01
New changeset 48744db70ed519c1566c22bf123a0e1f5c69253f by Raymond Hettinger in branch 'main':
bpo-45852:  Fix the Counter/iter test for statistics.mode() (GH-29667)
https://github.com/python/cpython/commit/48744db70ed519c1566c22bf123a0e1f5c69253f
msg406696 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2021-11-21 00:27
New changeset 9841ac2da5689ff765250c1abdbf5af9d3750519 by Miss Islington (bot) in branch '3.10':
bpo-45852:  Fix the Counter/iter test for statistics.mode() (GH-29667) (GH-29671)
https://github.com/python/cpython/commit/9841ac2da5689ff765250c1abdbf5af9d3750519
History
Date User Action Args
2022-04-11 14:59:52adminsetgithub: 90010
2021-11-21 00:27:53rhettingersetmessages: + msg406696
2021-11-20 17:01:18miss-islingtonsetnosy: + miss-islington

pull_requests: + pull_request27910
2021-11-20 17:01:14rhettingersetmessages: + msg406674
2021-11-20 16:38:56rhettingersetassignee: rhettinger
2021-11-20 16:37:27rhettingersetstatus: open -> closed
versions: + Python 3.11
messages: + msg406673

resolution: fixed
stage: patch review -> resolved
2021-11-20 16:36:38rhettingersetkeywords: + patch
stage: patch review
pull_requests: + pull_request27908
2021-11-20 15:28:41AlexWaygoodsetnosy: + rhettinger, steven.daprano
type: behavior
2021-11-20 02:26:19Stefan Pochmanncreate