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

@ -24,6 +24,7 @@ from typing import NamedTuple, TypedDict
from typing import IO, TextIO, BinaryIO
from typing import Pattern, Match
from typing import Annotated, ForwardRef
from typing import TypeAlias
import abc
import typing
import weakref
@ -4176,6 +4177,45 @@ class AnnotatedTests(BaseTestCase):
self.assertEqual(X[int], List[Annotated[int, 5]])
class TypeAliasTests(BaseTestCase):
def test_canonical_usage_with_variable_annotation(self):
Alias: TypeAlias = Employee
def test_canonical_usage_with_type_comment(self):
Alias = Employee # type: TypeAlias
def test_cannot_instantiate(self):
with self.assertRaises(TypeError):
TypeAlias()
def test_no_isinstance(self):
with self.assertRaises(TypeError):
isinstance(42, TypeAlias)
def test_no_issubclass(self):
with self.assertRaises(TypeError):
issubclass(Employee, TypeAlias)
with self.assertRaises(TypeError):
issubclass(TypeAlias, Employee)
def test_cannot_subclass(self):
with self.assertRaises(TypeError):
class C(TypeAlias):
pass
with self.assertRaises(TypeError):
class C(type(TypeAlias)):
pass
def test_repr(self):
self.assertEqual(repr(TypeAlias), 'typing.TypeAlias')
def test_cannot_subscript(self):
with self.assertRaises(TypeError):
TypeAlias[int]
class AllTests(BaseTestCase):
"""Tests for __all__."""