[New-bugs-announce] [issue45852] statistics.mode test doesn't test what it claims to

Stefan Pochmann report at bugs.python.org
Fri Nov 19 21:26:19 EST 2021


New submission from Stefan Pochmann <stefan.pochmann at gmail.com>:

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)

----------
components: Tests
messages: 406642
nosy: Stefan Pochmann
priority: normal
severity: normal
status: open
title: statistics.mode test doesn't test what it claims to
versions: Python 3.10

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue45852>
_______________________________________


More information about the New-bugs-announce mailing list