mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
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:
parent
f90dc36c15
commit
4f3c25043d
6 changed files with 94 additions and 2 deletions
|
@ -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__."""
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue