8000 gh-96769: Cover more typing special forms to be unsubclassable · python/cpython@e655315 · GitHub
[go: up one dir, main page]

Skip to content

Commit e655315

Browse files
committed
gh-96769: Cover more typing special forms to be unsubclassable
1 parent 53a54b7 commit e655315

File tree

1 file changed

+38
-5
lines changed

1 file changed

+38
-5
lines changed

Lib/test/test_typing.py

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,6 +1037,11 @@ class C(TypeVarTuple): pass
10371037
with self.assertRaisesRegex(TypeError,
10381038
CANNOT_SUBCLASS_INSTANCE % 'TypeVarTuple'):
10391039
class C(Ts): pass
1040+
with self.assertRaisesRegex(TypeError, r'Cannot subclass \*Ts'):
1041+
class C(*Ts): pass
1042+
with self.assertRaisesRegex(TypeError,
1043+
r'Cannot subclass typing\.Unpack'):
1044+
class C(Unpack): pass
10401045
with self.assertRaisesRegex(TypeError, r'Cannot subclass \*Ts'):
10411046
class C(Unpack[Ts]): pass
10421047

@@ -3710,6 +3715,14 @@ class C(type(ClassVar)):
37103715
with self.assertRaisesRegex(TypeError, CANNOT_SUBCLASS_TYPE):
37113716
class C(type(ClassVar[int])):
37123717
pass
3718+
with self.assertRaisesRegex(TypeError,
3719+
r'Cannot subclass typing\.ClassVar'):
3720+
class C(ClassVar):
3721+
pass
3722+
with self.assertRaisesRegex(TypeError,
3723+
r'Cannot subclass typing\.ClassVar\[int\]'):
3724+
class C(ClassVar[int]):
3725+
pass
37133726

37143727
def test_cannot_init(self):
37153728
with self.assertRaises(TypeError):
@@ -3752,6 +3765,14 @@ class C(type(Final)):
37523765
with self.assertRaisesRegex(TypeError, CANNOT_SUBCLASS_TYPE):
37533766
class C(type(Final[int])):
37543767
pass
3768+
with self.assertRaisesRegex(TypeError,
3769+
r'Cannot subclass typing\.Final'):
3770+
class C(Final):
3771+
pass
3772+
with self.assertRaisesRegex(TypeError,
3773+
r'Cannot subclass typing\.Final\[int\]'):
3774+
class C(Final[int]):
3775+
pass
37553776

37563777
def test_cannot_init(self):
37573778
with self.assertRaises(TypeError):
@@ -6439,13 +6460,18 @@ def test_re_submodule(self):
64396460
self.assertEqual(len(w), 1)
64406461

64416462
def test_cannot_subclass(self):
6442-
with self.assertRaises(TypeError) as ex:
6443-
6463+
with self.assertRaisesRegex(
6464+
TypeError,
6465+
r"type 're\.Match' is not an acceptable base type",
6466+
):
64446467
class A(typing.Match):
64456468
pass
6446-
6447-
self.assertEqual(str(ex.exception),
6448-
"type 're.Match' is not an acceptable base type")
6469+
with self.assertRaisesRegex(
6470+
TypeError,
6471+
r"type 're\.Pattern' is not an acceptable base type",
6472+
):
6473+
class A(typing.Pattern):
6474+
pass
64496475

64506476

64516477
class AnnotatedTests(BaseTestCase):
@@ -7037,6 +7063,13 @@ class C(type(TypeGuard)):
70377063
with self.assertRaisesRegex(TypeError, CANNOT_SUBCLASS_TYPE):
70387064
class C(type(TypeGuard[int])):
70397065
pass
7066+
with self.assertRaisesRegex(TypeError,
7067+
r'Cannot subclass typing\.TypeGuard'):
7068+
class C(TypeGuard):
7069+
pass
7070+
with self.assertRaisesRegex(TypeError, CANNOT_SUBCLASS_TYPE):
7071+
class C(type(TypeGuard[int])):
7072+
pass
70407073

70417074
def test_cannot_init(self):
70427075
with self.assertRaises(TypeError):

0 commit comments

Comments
 (0)
0