diff options
author | Nikita Sobolev <mail@sobolevn.me> | 2022-09-14 02:35:16 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-09-14 02:35:16 (GMT) |
commit | 9b3d2d008a6d8d6161c3a553f1e0fa76aa6a032e (patch) | |
tree | 7daca65a054ff11fa93230286bcb399a6749d238 /Lib/test | |
parent | 8dc9b3fbc1f09fdd7711157afe9c938bfb00153f (diff) | |
download | cpython-9b3d2d008a6d8d6161c3a553f1e0fa76aa6a032e.zip cpython-9b3d2d008a6d8d6161c3a553f1e0fa76aa6a032e.tar.gz cpython-9b3d2d008a6d8d6161c3a553f1e0fa76aa6a032e.tar.bz2 |
gh-96769: Cover more typing special forms to be unsubclassable (#96772)
Diffstat (limited to 'Lib/test')
-rw-r--r-- | Lib/test/test_typing.py | 48 |
1 files changed, 43 insertions, 5 deletions
diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index d68c325..d0f6a18 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -1038,6 +1038,15 @@ class TypeVarTupleTests(BaseTestCase): 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 def test_variadic_class_args_are_correct(self): @@ -3710,6 +3719,14 @@ class ClassVarTests(BaseTestCase): 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): @@ -3752,6 +3769,14 @@ class FinalTests(BaseTestCase): 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): @@ -6439,13 +6464,18 @@ class RETests(BaseTestCase): 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): @@ -7037,6 +7067,14 @@ class TypeGuardTests(BaseTestCase): 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): |