|
110 | 110 | KT = TypeVar("KT") |
111 | 111 | VT = TypeVar("VT") |
112 | 112 |
|
| 113 | +CANNOT_SUBCLASS_TYPE = 'Cannot subclass special typing classes' |
| 114 | +NOT_A_BASE_TYPE = r"type '(?:typing|typing_extensions).%s' is not an acceptable base type" |
| 115 | +CANNOT_SUBCLASS_INSTANCE = 'Cannot subclass an instance of %s' |
| 116 | + |
113 | 117 | # Flags used to mark tests that only apply after a specific |
114 | 118 | # version of the typing module. |
115 | 119 | TYPING_3_10_0 = sys.version_info[:3] >= (3, 10, 0) |
@@ -6792,6 +6796,27 @@ def test_pickle(self): |
6792 | 6796 | self.assertEqual(z.__name__, typevartuple.__name__) |
6793 | 6797 | self.assertEqual(z.__default__, typevartuple.__default__) |
6794 | 6798 |
|
| 6799 | + def test_cannot_subclass(self): |
| 6800 | + with self.assertRaisesRegex(TypeError, NOT_A_BASE_TYPE % 'TypeVarTuple'): |
| 6801 | + class C(TypeVarTuple): pass |
| 6802 | + Ts = TypeVarTuple('Ts') |
| 6803 | + with self.assertRaisesRegex(TypeError, |
| 6804 | + CANNOT_SUBCLASS_INSTANCE % 'TypeVarTuple'): |
| 6805 | + class D(Ts): pass |
| 6806 | + with self.assertRaisesRegex(TypeError, CANNOT_SUBCLASS_TYPE): |
| 6807 | + class E(type(Unpack)): pass |
| 6808 | + with self.assertRaisesRegex(TypeError, CANNOT_SUBCLASS_TYPE): |
| 6809 | + class F(type(*Ts)): pass |
| 6810 | + with self.assertRaisesRegex(TypeError, CANNOT_SUBCLASS_TYPE): |
| 6811 | + class G(type(Unpack[Ts])): pass |
| 6812 | + with self.assertRaisesRegex(TypeError, |
| 6813 | + r'Cannot subclass typing\.Unpack'): |
| 6814 | + class H(Unpack): pass |
| 6815 | + with self.assertRaisesRegex(TypeError, r'Cannot subclass typing.Unpack\[Ts\]'): |
| 6816 | + class I(*Ts): pass |
| 6817 | + with self.assertRaisesRegex(TypeError, r'Cannot subclass typing.Unpack\[Ts\]'): |
| 6818 | + class J(Unpack[Ts]): pass |
| 6819 | + |
6795 | 6820 |
|
6796 | 6821 | class FinalDecoratorTests(BaseTestCase): |
6797 | 6822 | def test_final_unmodified(self): |
|
0 commit comments