bpo-41923: PEP 613: Add TypeAlias to typing module (#22532)

This special marker annotation is intended to help in distinguishing
proper PEP 484-compliant type aliases from regular top-level variable
assignments.
This commit is contained in:
Mikhail Golubev 2020-10-08 00:44:31 +03:00 committed by GitHub
parent f90dc36c15
commit 4f3c25043d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 94 additions and 2 deletions

View file

@ -113,6 +113,7 @@ __all__ = [
'runtime_checkable',
'Text',
'TYPE_CHECKING',
'TypeAlias',
]
# The pseudo-submodules 're' and 'io' are part of the public
@ -460,6 +461,21 @@ def Literal(self, parameters):
return _GenericAlias(self, parameters)
@_SpecialForm
def TypeAlias(self, parameters):
"""Special marker indicating that an assignment should
be recognized as a proper type alias definition by type
checkers.
For example::
Predicate: TypeAlias = Callable[..., bool]
It's invalid when used anywhere except as in the example above.
"""
raise TypeError(f"{self} is not subscriptable")
class ForwardRef(_Final, _root=True):
"""Internal wrapper to hold a forward reference."""