Commit bfd68bf4 authored by Nick Coghlan's avatar Nick Coghlan

Issue #20478: avoid special casing Counter in statistics

Passing Counter objects to the Counter constructor is
special cased, going through iter() firsts ensures they
are handled the same way as any other iterable.

(Committing on Steven's behalf as I don't believe his
SSH key is registered yet)
parent ec1c8097
......@@ -268,9 +268,7 @@ def _coerce_types(T1, T2):
def _counts(data):
# Generate a table of sorted (value, frequency) pairs.
if data is None:
raise TypeError('None is not iterable')
table = collections.Counter(data).most_common()
table = collections.Counter(iter(data)).most_common()
if not table:
return table
# Extract the values with the highest frequency.
......
......@@ -1355,6 +1355,14 @@ class TestMode(NumericTestCase, AverageMixin, UnivariateTypeMixin):
# collections.Counter, which accepts None and returns an empty dict.
self.assertRaises(TypeError, self.func, None)
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)
# === Tests for variances and standard deviations ===
......
......@@ -24,6 +24,9 @@ Core and Builtins
Library
-------
- Issue #20478: the statistics module now treats collections.Counter inputs
like any other iterable.
- Issue #17369: get_filename was raising an exception if the filename
parameter's RFC2231 encoding was broken in certain ways. This was
a regression relative to python2.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment