[New-bugs-announce] [issue27605] Inconsistent calls to __eq__ from built-in __contains__

Vasiliy Faronov report at bugs.python.org
Sun Jul 24 08:13:15 EDT 2016


New submission from Vasiliy Faronov:

Consider the attached example program. I expect it to run successfully, because the Python 3 language reference says [1]:

> For container types such as list, tuple, set, frozenset, dict, or collections.deque, the expression `x in y` is equivalent to `any(x is e or x == e for e in y)`.

and [2]:

> `x==y` calls `x.__eq__(y)`

Instead, under Python 3.5.2, the program crashes with an assertion error, because `dict.__contains__` calls `Bar.__eq__` instead of `Foo.__eq__`.

The same happens if you replace the dict with a set or a frozenset. But if you replace it with a list or a tuple, the behavior is as documented.

This seems to me like a bug in either the implementation or the documentation.

The language reference clearly says [3] that equality should be symmetric "if possible", but is not required to be, and indeed that is hard to guarantee when different classes are involved.

[1] https://docs.python.org/3/reference/expressions.html#membership-test-details
[2] https://docs.python.org/3/reference/datamodel.html#object.__eq__
[3] https://docs.python.org/3/reference/expressions.html#value-comparisons

----------
files: contains_eq.py
messages: 271145
nosy: vfaronov
priority: normal
severity: normal
status: open
title: Inconsistent calls to __eq__ from built-in __contains__
type: behavior
Added file: http://bugs.python.org/file43857/contains_eq.py

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue27605>
_______________________________________


More information about the New-bugs-announce mailing list