Issue 6038: Should collections.Counter check for int? (original) (raw)

I noticed that while the docs say that "Counts are allowed to be any integer value including zero or negative counts", collections.Counter doesn't perform any check on the types of count values. Instead, non-numerical values will lead to strange behaviour or exceptions later on:

c = collections.Counter({'a':'3', 'b':'20', 'c':'100'}) c.most_common(2) [('a', '3'), ('b', '20')] c+c Traceback (most recent call last): File "", line 1, in File "/local/hagenf/lib/python3.1/collections.py", line 467, in add if newcount > 0: TypeError: unorderable types: str() > int()

I'd prefer Counter to refuse non-numerical values right away as the present behaviour may hide bugs (e.g. a forgotten string->int conversion). Any opinions? (And what about negative values or floats?)