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

Skip to content

Commit 9b3d2d0

Browse files
authored
gh-96769: Cover more typing special forms to be unsubclassable (#96772)
1 parent 8dc9b3f commit 9b3d2d0

File tree

1 file changed

+43
-5
lines changed

1 file changed

+43
-5
lines changed

Lib/test/test_typing.py

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,6 +1037,15 @@ 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, CANNOT_SUBCLASS_TYPE):
1043+
class C(type(Unpack)): pass
1044+
with self.assertRaisesRegex(TypeError, CANNOT_SUBCLASS_TYPE):
1045+
class C(type(Unpack[Ts])): pass
1046+
with self.assertRaisesRegex(TypeError,
1047+
r'Cannot subclass typing\.Unpack'):
1048+
class C(Unpack): pass
10401049
with self.assertRaisesRegex(TypeError, r'Cannot subclass \*Ts'):
10411050
class C(Unpack[Ts]): pass
10421051

@@ -3710,6 +3719,14 @@ class C(type(ClassVar)):
37103719
with self.assertRaisesRegex(TypeError, CANNOT_SUBCLASS_TYPE):
37113720
class C(type(ClassVar[int])):
37123721
pass
3722+
with self.assertRaisesRegex(TypeError,
3723+
r'Cannot subclass typing\.ClassVar'):
3724+
class C(ClassVar):
3725+
pass
3726+
with self.assertRaisesRegex(TypeError,
3727+
r'Cannot subclass typing\.ClassVar\[int\]'):
3728+
class C(ClassVar[int]):
3729+
pass
37133730

37143731
def test_cannot_init(self):
37153732
with self.assertRaises(TypeError):
@@ -3752,6 +3769,14 @@ class C(type(Final)):
37523769
with self.assertRaisesRegex(TypeError, CANNOT_SUBCLASS_TYPE):
37533770
class C(type(Final[int])):
37543771
pass
3772+
with self.assertRaisesRegex(TypeError,
3773+
r'Cannot subclass typing\.Final'):
3774+
class C(Final):
3775+
pass
3776+
with self.assertRaisesRegex(TypeError,
3777+
r'Cannot subclass typing\.Final\[int\]'):
3778+
class C(Final[int]):
3779+
pass
37553780

37563781
def test_cannot_init(self):
37573782
with self.assertRaises(TypeError):
@@ -6439,13 +6464,18 @@ def test_re_submodule(self):
64396464
self.assertEqual(len(w), 1)
64406465

64416466
def test_cannot_subclass(self):
6442-
with self.assertRaises(TypeError) as ex:
6443-
6467+
with self.assertRaisesRegex(
6468+
TypeError,
6469+
r"type 're\.Match' is not an acceptable base type",
6470+
):
64446471
class A(typing.Match):
64456472
pass
6446-
6447-
self.assertEqual(str(ex.exception),
6448-
"type 're.Match' is not an acceptable base type")
6473+
with self.assertRaisesRegex(
6474+
TypeError,
6475+
r"type 're\.Pattern' is not an acceptable base type",
6476+
):
6477+
class A(typing.Pattern):
6478+
pass
64496479

64506480

64516481
class AnnotatedTests(BaseTestCase):
@@ -7037,6 +7067,14 @@ class C(type(TypeGuard)):
70377067
with self.assertRaisesRegex(TypeError, CANNOT_SUBCLASS_TYPE):
70387068
class C(type(TypeGuard[int])):
70397069
pass
7070+
with self.assertRaisesRegex(TypeError,
7071+
r'Cannot subclass typing\.TypeGuard'):
7072+
class C(TypeGuard):
7073+
pass
7074+
with self.assertRaisesRegex(TypeError,
7075+
r'Cannot subclass typing\.TypeGuard\[int\]'):
7076+
class C(TypeGuard[int]):
7077+
pass
70407078

70417079
def test_cannot_init(self):
70427080
with self.assertRaises(TypeError):

0 commit comments

Comments
 (0)
0