[New-bugs-announce] [issue27399] ChainMap.keys() is broken

Zahari Dim report at bugs.python.org
Mon Jun 27 07:07:45 EDT 2016

New submission from Zahari Dim:

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)
    634     def __len__(self):
--> 635         return len(self._mapping)
    637     def __repr__(self):

/home/zah/anaconda3/lib/python3.5/collections/__init__.py in __len__(self)
    866     def __len__(self):
--> 867         return len(set().union(*self.maps))     # reuses stored hash values if possible
    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

components: Library (Lib)
messages: 269370
nosy: Zahari.Dim
priority: normal
severity: normal
status: open
title: ChainMap.keys() is broken
versions: Python 3.5

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list