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: ChainMap.keys() is broken
Type: Stage:
Components: Library (Lib) Versions: Python 3.5
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Zahari.Dim, rhettinger
Priority: normal Keywords:

Created on 2016-06-27 11:07 by Zahari.Dim, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (1)
msg269370 - (view) Author: Zahari Dim (Zahari.Dim) Date: 2016-06-27 11:07
When trying to see if the keys() of a collections.ChainMap object are empty, it tries to compute the hash of the dicts that compose the ChainMap, giving rise to an error:

In [1]: from collections import ChainMap

In [2]: m = ChainMap([{'a':1}, {'b':2}])

In [3]: bool(m.keys())
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-365f7b817ebf> in <module>()
----> 1 bool(m.keys())

/home/zah/anaconda3/lib/python3.5/_collections_abc.py in __len__(self)
    633 
    634     def __len__(self):
--> 635         return len(self._mapping)
    636 
    637     def __repr__(self):

/home/zah/anaconda3/lib/python3.5/collections/__init__.py in __len__(self)
    865 
    866     def __len__(self):
--> 867         return len(set().union(*self.maps))     # reuses stored hash values if possible
    868 
    869     def __iter__(self):

TypeError: unhashable type: 'dict'

Also, I can't ask if 'a' is in keys:

In [6]: m.keys()
Out[6]: KeysView(ChainMap([{'a': 1}, {'b': 2}]))
In [9]: ks = m.keys()
In [17]: 'a' in ks
Out[17]: False
History
Date User Action Args
2022-04-11 14:58:33adminsetgithub: 71586
2016-06-27 13:30:35r.david.murraysetnosy: + rhettinger
2016-06-27 11:13:00Zahari.Dimsetstatus: open -> closed
resolution: not a bug
2016-06-27 11:07:45Zahari.Dimcreate