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: "set() in set()" should raise TypeError: unhashable type: 'set'
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.10, Python 3.9, Python 3.8
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: remi.lapeyre, rhettinger, yesheng
Priority: normal Keywords:

Created on 2020-06-03 10:26 by yesheng, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (5)
msg370652 - (view) Author: (yesheng) Date: 2020-06-03 10:26
>>> set() in set()
False  # should raise TypeError

>>> dict() in set()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'dict'

>>> set() in dict()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'set'

>>> dict() in dict()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'dict'

>>> frozenset({1,2}) in {frozenset({1,2}), 3}
True

>>> {1,2} in {frozenset({1,2}), 3}
True  # should raise TypeError
msg370653 - (view) Author: Rémi Lapeyre (remi.lapeyre) * Date: 2020-06-03 10:38
The set is converted to a frozenset before the comparison so 

>>> {1,2} in {frozenset({1,2}), 3}

is equivalent to 

>>> frozenset({1,2}) in {frozenset({1,2}), 3}


This was introduced on purpose 17 years ago in 19c2d778 so it's unlikely to change.
msg370654 - (view) Author: Rémi Lapeyre (remi.lapeyre) * Date: 2020-06-03 10:59
This is documented in https://docs.python.org/3.8/library/stdtypes.html#set-types-set-frozenset:


Instances of set are compared to instances of frozenset based on their members. For example, set('abc') == frozenset('abc') returns True and so does set('abc') in set([frozenset('abc')]).
msg370657 - (view) Author: (yesheng) Date: 2020-06-03 11:13
thanks, it sounds reasonable.
is it possible to make dict give consistent result?
{1,2} in {frozenset({1,2}):3} 
# to return True
msg370677 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2020-06-03 15:57
> is it possible to make dict give consistent result?

That is unlikely (there isn't much need, it would introduce a cross-type dependency, it would be awkward dicts which use keys in many more ways than sets do).

For the time being, a user should explicitly convert to a frozenset:

    frozenset(s) in d
History
Date User Action Args
2022-04-11 14:59:31adminsetgithub: 85030
2020-06-03 15:57:23rhettingersetstatus: open -> closed
resolution: not a bug
messages: + msg370677

stage: resolved
2020-06-03 11:13:17yeshengsetmessages: + msg370657
2020-06-03 10:59:51remi.lapeyresetmessages: + msg370654
2020-06-03 10:39:17remi.lapeyresetnosy: + rhettinger

components: + Interpreter Core
versions: + Python 3.9, Python 3.10
2020-06-03 10:38:38remi.lapeyresetnosy: + remi.lapeyre
messages: + msg370653
2020-06-03 10:26:55yeshengcreate