Skip to content

Commit 0774aec

Browse files
Fix error message when subclassing TypeVarTuple
1 parent 9215c95 commit 0774aec

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Unreleased
22

3+
- Fix error message when trying to subclass `typing_extensions.TypeVarTuple`.
34
- Fix incorrect behaviour on Python 3.9 and Python 3.10 that meant that
45
calling `isinstance` with `typing_extensions.Concatenate[...]` or
56
`typing_extensions.Unpack[...]` as the first argument could have a different

src/test_typing_extensions.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@
110110
KT = TypeVar("KT")
111111
VT = TypeVar("VT")
112112

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+
113117
# Flags used to mark tests that only apply after a specific
114118
# version of the typing module.
115119
TYPING_3_10_0 = sys.version_info[:3] >= (3, 10, 0)
@@ -6792,6 +6796,27 @@ def test_pickle(self):
67926796
self.assertEqual(z.__name__, typevartuple.__name__)
67936797
self.assertEqual(z.__default__, typevartuple.__default__)
67946798

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+
67956820

67966821
class FinalDecoratorTests(BaseTestCase):
67976822
def test_final_unmodified(self):

src/typing_extensions.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2637,7 +2637,9 @@ def _typevartuple_prepare_subst(alias, args):
26372637
return tvt
26382638

26392639
def __init_subclass__(self, *args, **kwds):
2640-
raise TypeError("Cannot subclass special typing classes")
2640+
raise TypeError(
2641+
f"type '{__name__}.TypeVarTuple' is not an acceptable base type"
2642+
)
26412643

26422644
else: # <=3.10
26432645
class TypeVarTuple(_DefaultMixin):

0 commit comments

Comments
 (0)