classification
Title: collections.ChainMap.__iter__ calls __getitem__ on underlying maps
Type: performance Stage: resolved
Components: Library (Lib) Versions: Python 3.10, Python 3.9
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: ap--, lukasz.langa, miss-islington, rhettinger, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2020-11-28 00:50 by ap--, last changed 2020-11-30 21:23 by rhettinger. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 23534 merged ap--, 2020-11-28 01:00
PR 23569 merged miss-islington, 2020-11-30 16:35
Messages (4)
msg381971 - (view) Author: Andreas Poehlmann (ap--) * Date: 2020-11-28 00:50
Hello,

I encountered an issue with collections.ChainMap, that was introduced when https://bugs.python.org/issue32792 got fixed.

Iterating a ChainMap will call __getitem__ on its underlying maps:


>>> from collections import UserDict, ChainMap
>>> class MyDict(UserDict):
...     def __getitem__(self, k):
...         print("expensive computation", k)
...         return super().__getitem__(k)
... 
>>> set(ChainMap(MyDict(a=1, b=2, c=3)))
expensive computation a
expensive computation b
expensive computation c
{'c', 'b', 'a'}
msg381974 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2020-11-28 01:49
I had a discussion with the release manager and we're good to backport to 3.9 but not 3.8.

Serhiy, would you also take a look at this?
msg382159 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2020-11-30 16:34
New changeset 0be9ce305ff2b9e13ddcf15af8cfe28786afb36a by Andreas Poehlmann in branch 'master':
bpo-42487: don't call __getitem__ of underlying maps in ChainMap.__iter__ (GH-23534)
https://github.com/python/cpython/commit/0be9ce305ff2b9e13ddcf15af8cfe28786afb36a
msg382164 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2020-11-30 17:07
New changeset cf22aa3bc698847459a6bf20c166aa80825b810a by Miss Islington (bot) in branch '3.9':
bpo-42487: don't call __getitem__ of underlying maps in ChainMap.__iter__ (GH-23534) (GH-23569)
https://github.com/python/cpython/commit/cf22aa3bc698847459a6bf20c166aa80825b810a
History
Date User Action Args
2020-11-30 21:23:39rhettingersetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2020-11-30 17:07:34rhettingersetmessages: + msg382164
2020-11-30 16:35:12miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request22450
2020-11-30 16:34:24rhettingersetmessages: + msg382159
2020-11-28 01:49:51rhettingersetversions: + Python 3.9
nosy: + serhiy.storchaka, lukasz.langa, rhettinger

messages: + msg381974

assignee: rhettinger
2020-11-28 01:20:22rhettingersettype: behavior -> performance
versions: - Python 3.7, Python 3.8, Python 3.9
2020-11-28 01:00:31ap--setkeywords: + patch
stage: patch review
pull_requests: + pull_request22416
2020-11-28 00:50:30ap--create