Skip to content

gh-96769: Cover more typing special forms to be unsubclassable #96772

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 14, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 43 additions & 5 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1037,6 +1037,15 @@ class C(TypeVarTuple): pass
with self.assertRaisesRegex(TypeError,
CANNOT_SUBCLASS_INSTANCE % 'TypeVarTuple'):
class C(Ts): pass
with self.assertRaisesRegex(TypeError, r'Cannot subclass \*Ts'):
class C(*Ts): pass
with self.assertRaisesRegex(TypeError, CANNOT_SUBCLASS_TYPE):
class C(type(Unpack)): pass
with self.assertRaisesRegex(TypeError, CANNOT_SUBCLASS_TYPE):
class C(type(Unpack[Ts])): pass
with self.assertRaisesRegex(TypeError,
r'Cannot subclass typing\.Unpack'):
class C(Unpack): pass
with self.assertRaisesRegex(TypeError, r'Cannot subclass \*Ts'):
class C(Unpack[Ts]): pass

Expand Down Expand Up @@ -3710,6 +3719,14 @@ class C(type(ClassVar)):
with self.assertRaisesRegex(TypeError, CANNOT_SUBCLASS_TYPE):
class C(type(ClassVar[int])):
pass
with self.assertRaisesRegex(TypeError,
r'Cannot subclass typing\.ClassVar'):
class C(ClassVar):
pass
with self.assertRaisesRegex(TypeError,
r'Cannot subclass typing\.ClassVar\[int\]'):
class C(ClassVar[int]):
pass

def test_cannot_init(self):
with self.assertRaises(TypeError):
Expand Down Expand Up @@ -3752,6 +3769,14 @@ class C(type(Final)):
with self.assertRaisesRegex(TypeError, CANNOT_SUBCLASS_TYPE):
class C(type(Final[int])):
pass
with self.assertRaisesRegex(TypeError,
r'Cannot subclass typing\.Final'):
class C(Final):
pass
with self.assertRaisesRegex(TypeError,
r'Cannot subclass typing\.Final\[int\]'):
class C(Final[int]):
pass

def test_cannot_init(self):
with self.assertRaises(TypeError):
Expand Down Expand Up @@ -6439,13 +6464,18 @@ def test_re_submodule(self):
self.assertEqual(len(w), 1)

def test_cannot_subclass(self):
with self.assertRaises(TypeError) as ex:

with self.assertRaisesRegex(
TypeError,
r"type 're\.Match' is not an acceptable base type",
):
class A(typing.Match):
pass

self.assertEqual(str(ex.exception),
"type 're.Match' is not an acceptable base type")
with self.assertRaisesRegex(
TypeError,
r"type 're\.Pattern' is not an acceptable base type",
):
class A(typing.Pattern):
pass


class AnnotatedTests(BaseTestCase):
Expand Down Expand Up @@ -7037,6 +7067,14 @@ class C(type(TypeGuard)):
with self.assertRaisesRegex(TypeError, CANNOT_SUBCLASS_TYPE):
class C(type(TypeGuard[int])):
pass
with self.assertRaisesRegex(TypeError,
r'Cannot subclass typing\.TypeGuard'):
class C(TypeGuard):
pass
with self.assertRaisesRegex(TypeError,
r'Cannot subclass typing\.TypeGuard\[int\]'):
class C(TypeGuard[int]):
pass

def test_cannot_init(self):
with self.assertRaises(TypeError):
Expand Down