Issue 22801: collections.Counter, when empty, doesn't raise an error with &= when other is an incompatible type (original) (raw)

Issue22801

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 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:09 admin set github: 66990
2015-03-03 05:33:53 rhettinger set status: open -> closedresolution: rejectedmessages: +
2015-03-02 09:12:08 serhiy.storchaka set nosy: + serhiy.storchakamessages: +
2015-03-02 08:38:34 ezio.melotti set stage: patch review -> test needed
2014-11-05 17:47:46 ethan.furman set files: + issue22801.stoneleaf.01.patch
2014-11-05 17:45:50 ethan.furman set files: - issue22778.stoneleaf.01.patch
2014-11-05 17:44:31 ethan.furman create