-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
More differences in instance and subclass checks between typing.Union and types.Union #88834
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
Comments
>>> import typing
>>> T = typing.TypeVar('T')
>>> issubclass(int, int | T | str)
True
>>> issubclass(int, str | T | int)
True
>>> issubclass(int, typing.Union[int, T, str])
True
>>> issubclass(int, typing.Union[str, T, int])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/serhiy/py/cpython/Lib/typing.py", line 1208, in __subclasscheck__
if issubclass(cls, arg):
^^^^^^^^^^^^^^^^^^^^
TypeError: issubclass() arg 2 must be a class, a tuple of classes, or a union.
>>> isinstance(1, int | T | str)
True
>>> isinstance(1, str | T | int)
True
>>> isinstance(1, typing.Union[int, T, str])
True
>>> isinstance(1, typing.Union[str, T, int])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/serhiy/py/cpython/Lib/typing.py", line 1204, in __instancecheck__
return self.__subclasscheck__(type(obj))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/serhiy/py/cpython/Lib/typing.py", line 1208, in __subclasscheck__
if issubclass(cls, arg):
^^^^^^^^^^^^^^^^^^^^
TypeError: issubclass() arg 2 must be a class, a tuple of classes, or a union.
|
This seems a low-priority issue to me. The code is invalid, but I don't think it's very important to raise an exception (typing.Union is also inconsistent). Static type checkers will flag all these as errors. (Or perhaps none of them? It may depend on the meaning of T in the current scope.) |
It is easy to fix once we define the correct behavior in corner cases. |
Sure. |
This looks to have been fixed at some point, though I don't know when. >>> import typing
>>> T = typing.TypeVar("T")
>>> issubclass(int, int | T | str)
True
>>> issubclass(int, str | T | int)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\alexw\coding\cpython\Lib\typing.py", line 1698, in __subclasscheck__
if issubclass(cls, arg):
^^^^^^^^^^^^^^^^^^^^
TypeError: issubclass() arg 2 must be a class, a tuple of classes, or a union
>>> issubclass(int, typing.Union[int, T, str])
True
>>> issubclass(int, typing.Union[str, T, int])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\alexw\coding\cpython\Lib\typing.py", line 1698, in __subclasscheck__
if issubclass(cls, arg):
^^^^^^^^^^^^^^^^^^^^
TypeError: issubclass() arg 2 must be a class, a tuple of classes, or a union
>>> isinstance(1, int | T | str)
True
>>> isinstance(1, str | T | int)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\alexw\coding\cpython\Lib\typing.py", line 1694, in __instancecheck__
return self.__subclasscheck__(type(obj))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\alexw\coding\cpython\Lib\typing.py", line 1698, in __subclasscheck__
if issubclass(cls, arg):
^^^^^^^^^^^^^^^^^^^^
TypeError: issubclass() arg 2 must be a class, a tuple of classes, or a union
>>> isinstance(1, typing.Union[int, T, str])
True
>>> isinstance(1, typing.Union[str, T, int])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\alexw\coding\cpython\Lib\typing.py", line 1694, in __instancecheck__
return self.__subclasscheck__(type(obj))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\alexw\coding\cpython\Lib\typing.py", line 1698, in __subclasscheck__
if issubclass(cls, arg):
^^^^^^^^^^^^^^^^^^^^
TypeError: issubclass() arg 2 must be a class, a tuple of classes, or a union Depending on whether it was fixed "on purpose" or "by accident", we may need to add regression tests. |
…nionType Union now uses the instance checks against its parameters instead of the subclass checks.
…pe (GH-128363) Union now uses the instance checks against its parameters instead of the subclass checks.
…nionType (pythonGH-128363) Union now uses the instance checks against its parameters instead of the subclass checks. (cherry picked from commit b2ac70a) Co-authored-by: Serhiy Storchaka <[email protected]>
…nionType (pythonGH-128363) Union now uses the instance checks against its parameters instead of the subclass checks. (cherry picked from commit b2ac70a) Co-authored-by: Serhiy Storchaka <[email protected]>
…UnionType (GH-128363) (GH-128371) Union now uses the instance checks against its parameters instead of the subclass checks. (cherry picked from commit b2ac70a) Co-authored-by: Serhiy Storchaka <[email protected]>
…UnionType (GH-128363) (GH-128370) Union now uses the instance checks against its parameters instead of the subclass checks. (cherry picked from commit b2ac70a) Co-authored-by: Serhiy Storchaka <[email protected]>
…nionType (pythonGH-128363) Union now uses the instance checks against its parameters instead of the subclass checks.
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:
Linked PRs
The text was updated successfully, but these errors were encountered: