X Tutup
The Wayback Machine - https://web.archive.org/web/20250728180812/https://github.com/python/cpython/issues/114159
Skip to content

PEP 695 type aliases not suitable as replacement for typing.TypeAlias #114159

@mjog

Description

@mjog

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      X Tutup