You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
It is possible to create a 1-type union type #88802
bpo-44632: Union with TypeVar does not work as intended
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=<Date2021-07-16.11:49:32.820>created_at=<Date2021-07-14.11:04:42.002>labels= ['interpreter-core', 'type-bug', '3.10', '3.11']
title='It is possible to create a 1-type union type'updated_at=<Date2021-07-16.11:49:32.820>user='https://github.com/serhiy-storchaka'
I feel that keeping the singleton is more consistent. This normalization seems to me an example of the typing module doing too much. Singleton tuples are different from scalar values too.
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.
Activity
serhiy-storchaka commentedon Jul 14, 2021
typing.Union always collapsed to a simple type if it contains a single type.
But the builtin union type can contain a single type:
serhiy-storchaka commentedon Jul 15, 2021
Simple example (without indexing):
gvanrossum commentedon Jul 15, 2021
I feel that keeping the singleton is more consistent. This normalization seems to me an example of the typing module doing too much. Singleton tuples are different from scalar values too.
serhiy-storchaka commentedon Jul 15, 2021
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:
Also it breaks one of fundamental properties -- equal objects should have equal hashes.
gvanrossum commentedon Jul 15, 2021
Hm, you are right. Make it so, Mr. Spock! :-)
serhiy-storchaka commentedon Jul 16, 2021
New changeset d9f9232 by Serhiy Storchaka in branch 'main':
bpo-44636: Collapse union of equal types (GH-27178)
d9f9232
serhiy-storchaka commentedon Jul 16, 2021
New changeset c3007ab by Serhiy Storchaka in branch '3.10':
[3.10] bpo-44636: Collapse union of equal types (GH-27178) (GH-27181)
c3007ab