summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_typing.py
diff options
context:
space:
mode:
authorJames Hilton-Balfe <gobot1234yt@gmail.com>2022-02-07 20:47:48 (GMT)
committerGitHub <noreply@github.com>2022-02-07 20:47:48 (GMT)
commit7ba1cc8049fbcb94ac039ab02522f78177130588 (patch)
tree3b1a480d5512a3ad5e7f47c78db1458ba0ba1bee /Lib/test/test_typing.py
parent39dec1c09c9f5ddf951bed5b875f837735a06733 (diff)
downloadcpython-7ba1cc8049fbcb94ac039ab02522f78177130588.zip
cpython-7ba1cc8049fbcb94ac039ab02522f78177130588.tar.gz
cpython-7ba1cc8049fbcb94ac039ab02522f78177130588.tar.bz2
bpo-46534: Implement PEP 673 Self in typing.py (GH-30924)
Co-authored-by: Pradeep Kumar Srinivasan <gohanpra@gmail.com> Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Diffstat (limited to 'Lib/test/test_typing.py')
-rw-r--r--Lib/test/test_typing.py43
1 files changed, 42 insertions, 1 deletions
diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py
index 9eab461..a37bb43 100644
--- a/Lib/test/test_typing.py
+++ b/Lib/test/test_typing.py
@@ -27,6 +27,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 Self
from typing import TypeAlias
from typing import ParamSpec, Concatenate, ParamSpecArgs, ParamSpecKwargs
from typing import TypeGuard
@@ -157,8 +158,48 @@ class NoReturnTests(BaseTestCase):
type(NoReturn)()
-class TypeVarTests(BaseTestCase):
+class SelfTests(BaseTestCase):
+ def test_basics(self):
+ class Foo:
+ def bar(self) -> Self: ...
+
+ self.assertEqual(gth(Foo.bar), {'return': Self})
+
+ def test_repr(self):
+ self.assertEqual(repr(Self), 'typing.Self')
+
+ def test_cannot_subscript(self):
+ with self.assertRaises(TypeError):
+ Self[int]
+
+ def test_cannot_subclass(self):
+ with self.assertRaises(TypeError):
+ class C(type(Self)):
+ pass
+
+ def test_cannot_init(self):
+ with self.assertRaises(TypeError):
+ Self()
+ with self.assertRaises(TypeError):
+ type(Self)()
+
+ def test_no_isinstance(self):
+ with self.assertRaises(TypeError):
+ isinstance(1, Self)
+ with self.assertRaises(TypeError):
+ issubclass(int, Self)
+ def test_alias(self):
+ # TypeAliases are not actually part of the spec
+ alias_1 = Tuple[Self, Self]
+ alias_2 = List[Self]
+ alias_3 = ClassVar[Self]
+ self.assertEqual(get_args(alias_1), (Self, Self))
+ self.assertEqual(get_args(alias_2), (Self,))
+ self.assertEqual(get_args(alias_3), (Self,))
+
+
+class TypeVarTests(BaseTestCase):
def test_basic_plain(self):
T = TypeVar('T')
# T equals itself.