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: collections.Counter, when empty, doesn't raise an error with &= when other is an incompatible type
Type: behavior Stage: test needed
Components: Versions: Python 3.4, Python 3.5
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: ethan.furman, rhettinger, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2014-11-05 17:44 by ethan.furman, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
issue22801.stoneleaf.01.patch ethan.furman, 2014-11-05 17:47 review
Messages (3)
msg230699 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2014-11-05 17:44
test script:
---------------------------------------
from collections import Counter
empty_counter = Counter()
counter = Counter('abbc')
empty_counter &= 5
counter &= 5
---------------------------------------

results:
---------------------------------------
Traceback (most recent call last):
  File "blah.py", line 5, in <module>
    counter &= 5
  File "/home/ethan/source/python/issue22778/Lib/collections/__init__.py", line 780, in __iand__
    other_count = other[elem]
TypeError: 'int' object is not subscriptable
----------------------------------------

As can be seen, the error does not show up when the Counter is empty, which could lead to hard to diagnose bugs.
msg237035 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2015-03-02 09:12
Additional check is not for free.

$ ./python -m timeit -s "from collections import Counter; a = Counter(); b = Counter(range(10))" -- "a &= b"

Unpatched: 100000 loops, best of 3: 8.4 usec per loop
Patched:   100000 loops, best of 3: 9.7 usec per loop
msg237107 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2015-03-03 05:33
I see no reason pure python code to have to detect and report such oddities.
Closing as not worth it.
History
Date User Action Args
2022-04-11 14:58:09adminsetgithub: 66990
2015-03-03 05:33:53rhettingersetstatus: open -> closed
resolution: rejected
messages: + msg237107
2015-03-02 09:12:08serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg237035
2015-03-02 08:38:34ezio.melottisetstage: patch review -> test needed
2014-11-05 17:47:46ethan.furmansetfiles: + issue22801.stoneleaf.01.patch
2014-11-05 17:45:50ethan.furmansetfiles: - issue22778.stoneleaf.01.patch
2014-11-05 17:44:31ethan.furmancreate