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.

Author HuangFuSL
Recipients HuangFuSL
Date 2021-01-29.14:50:24
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1611931825.03.0.205807042614.issue43062@roundup.psfhosted.org>
In-reply-to
Content
When I am creating a counter object provided by `collections.Counter` using a mapping object like a dictionary, it seems that Python will not check the validity of the values in the mapping object.

I've checked the following Python script could be successfully executed using Python 3.9.0 on Windows.

```python
>>> from collections import Counter
>>> a = Counter({'0': '0'})
>>> a.elements()
<itertools.chain object at 0x00000252DDB5A4F0>
```

`a.elements()` returns a iterator, iterating through it will normally get the records counted by the `Counter`, but with a `str` object inside, iterating through it will make a `TypeError` raised.

```python
>>> for i in a.elements():
...     pass
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object cannot be interpreted as an integer
```

Meanwhile, if the counter contains values that cannot be compared such as `False` and `''`, `most_common()` method will fail.

```python
>>> b = Counter({'0': False, '1': ''})
>>> b.most_common()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python39\lib\collections\__init__.py", line 610, in most_common
    return sorted(self.items(), key=_itemgetter(1), reverse=True)
TypeError: '<' not supported between instances of 'bool' and 'str'
```

The `sys.version` variable of my Python interpreter is as follows:

```
>>> import sys
>>> sys.version
'3.9.0 (tags/v3.9.0:9cf6752, Oct  5 2020, 15:34:40) [MSC v.1927 64 bit (AMD64)]'
>>>
```

I'm not sure whether the result is intentionally designed, but I think such execution results may lead to confusion.
History
Date User Action Args
2021-01-29 14:50:25HuangFuSLsetrecipients: + HuangFuSL
2021-01-29 14:50:25HuangFuSLsetmessageid: <1611931825.03.0.205807042614.issue43062@roundup.psfhosted.org>
2021-01-29 14:50:25HuangFuSLlinkissue43062 messages
2021-01-29 14:50:24HuangFuSLcreate