-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
Open
Labels
Description
Bug report
Bug description:
PEP 695 type aliases are the blessed replacement for the now-deprecated typing.TypeAlias, but are functionally not suitable as replacement.
A type alias designated as such using the typing.TypeAlias annotation could be used in many places that a PEP 695 type aliases cannot, including (at least) as a class's parent class, in calls to isinstance and issubclass. All of of these have been used in various codebases I maintain.
As such typing.TypeAlias should be un-deprecated, or the implementation of PEP 695 type aliases should be updated to work in all cases where a plain type should be.
With typing.TypeAlias :
$ poetry run python3
Python 3.12.0 (main, Oct 4 2023, 06:27:34) [GCC 13.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import typing
>>> StringAlias: typing.TypeAlias = str
>>> isinstance('test', StringAlias)
True
>>> issubclass(StringAlias, str)
True
>>> class GreaterString(StringAlias): pass
...
>>> With PEP 695 type aliases:
$ poetry run python3
Python 3.12.0 (main, Oct 4 2023, 06:27:34) [GCC 13.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> type StringAlias = str
>>> isinstance('test', StringAlias)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: isinstance() arg 2 must be a type, a tuple of types, or a union
>>> issubclass(StringAlias, str)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: issubclass() arg 1 must be a class
>>> class GreaterString(StringAlias): pass
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: typealias() takes exactly 2 positional arguments (3 given)
>>> CPython versions tested on:
3.12
Operating systems tested on:
Linux
ods, PYEEDM, q0w, zhukovgreen, rafalkrupinski and 1 more

