[New-bugs-announce] [issue32675] dict.__contains__(unhashable) raises TypeError where False was expected

xitop report at bugs.python.org
Fri Jan 26 05:43:55 EST 2018

New submission from xitop <reg.bugs at poti.sk>:

The `dict.__contains()` function does not fully comply with the description in the "Data Model" section of the official documentation, which states:
  "__contains__(self, item) ... Should return true if item is in self, false otherwise."

Basically the same is written in "Mapping Types - dict": "key in d - Return True if d has a key key, else False."

According to that, testing a presence of an unhashable object in a dict should return False. But it raises a TypeError instead.

  >>> () in {}
  >>> [] in {}
  Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
  TypeError: unhashable type: 'list'

There is a related SO question:
currently without a clear answer whether it is a bug or desired behaviour supposed to expose programming errors. In the latter case, it seems to be not documented.

components: Interpreter Core
messages: 310751
nosy: xitop
priority: normal
severity: normal
status: open
title: dict.__contains__(unhashable) raises TypeError where False was expected
versions: Python 3.6

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list