New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
It is possible to create a 1-type union type #88802
Comments
typing.Union always collapsed to a simple type if it contains a single type. >>> import typing
>>> typing.Union[int, typing.T][int]
<class 'int'> But the builtin union type can contain a single type: >>> (int | typing.T)[int]
int
>>> type((int | typing.T)[int])
<class 'types.Union'> |
Simple example (without indexing): >>> import typing
>>> typing.Union[int, int]
<class 'int'>
>>> int | int
int |
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. >>> (int | int) == int
True
>>> hash(int | int) == hash(int)
False |
Hm, you are right. Make it so, Mr. Spock! :-) |
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:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: