Skip to content

Commit 21a36aa

Browse files
authored
gh-83731: Test that isinstance() ignores tuple subclass methods (#158000)
Add a regression test to make sure isinstance() reads the items of a tuple subclass directly, without calling its __iter__(), __getitem__() or __len__().
1 parent d52dba3 commit 21a36aa

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

‎Lib/test/test_isinstance.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,20 @@ def test_subclass_tuple(self):
263263
self.assertEqual(True, issubclass(int, (int, (float, int))))
264264
self.assertEqual(True, issubclass(str, (str, (Child, str))))
265265

266+
def test_tuple_subclass_methods_not_called(self):
267+
# gh-83731: make sure isinstance() reads the items of a tuple
268+
# subclass directly, without calling its __iter__(),
269+
# __getitem__() or __len__().
270+
class T(tuple):
271+
def __iter__(self):
272+
raise AssertionError('__iter__ called')
273+
def __getitem__(self, index):
274+
raise AssertionError('__getitem__ called')
275+
def __len__(self):
276+
raise AssertionError('__len__ called')
277+
278+
self.assertTrue(isinstance(1, T((int,))))
279+
266280
@support.skip_if_huge_c_stack()
267281
@support.skip_wasi_stack_overflow()
268282
@support.skip_emscripten_stack_overflow()

0 commit comments

Comments
 (0)