diff options
author | Mikhail Golubev <qsolo825@gmail.com> | 2020-10-07 21:44:31 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-10-07 21:44:31 (GMT) |
commit | 4f3c25043d651a13c41cffcee703f7d5cb677cc7 (patch) | |
tree | 08093971ec6aefd8b762e8da9d20434c09a3c3ae /Lib/test | |
parent | f90dc36c15d7fee0efaf6d39e97be0bdf2683e93 (diff) | |
download | cpython-4f3c25043d651a13c41cffcee703f7d5cb677cc7.zip cpython-4f3c25043d651a13c41cffcee703f7d5cb677cc7.tar.gz cpython-4f3c25043d651a13c41cffcee703f7d5cb677cc7.tar.bz2 |
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.
Diffstat (limited to 'Lib/test')
-rw-r--r-- | Lib/test/test_typing.py | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index 4bef42f..57dd73c 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -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__.""" |