[issue44636] It is possible to create a 1-type union type

Serhiy Storchaka report at bugs.python.org
Thu Jul 15 12:20:01 EDT 2021


Serhiy Storchaka <storchaka+cpython at gmail.com> added the comment:

It is a difference with typing.Union which can cause confusion. If the union type is like a tuple and we leave a 1-type union, why do we bother with deduplication? Why int | str | int is collapsed into int | str?

Also it complicates the comparison implementation and produces surprising exceptions:

>>> int | str == {}
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'dict'

Also it breaks one of fundamental properties -- equal objects should have equal hashes.

>>> (int | int) == int
True
>>> hash(int | int) == hash(int)
False

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue44636>
_______________________________________


More information about the Python-bugs-list mailing list