From 12409e9e66a895bc9040f3fad4b869763f4695ce Mon Sep 17 00:00:00 2001 From: Vasiliy Kiryanov Date: Fri, 16 Oct 2026 00:16:11 -0400 Subject: [PATCH] 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__(). (cherry picked from commit 21a36aa79ca0b2af49995fa1b9216b01ef98f195) --- Lib/test/test_isinstance.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/Lib/test/test_isinstance.py b/Lib/test/test_isinstance.py index 95a119ba683e09e..c7faf7a5759c23e 100644 --- a/Lib/test/test_isinstance.py +++ b/Lib/test/test_isinstance.py @@ -263,6 +263,20 @@ def test_subclass_tuple(self): self.assertEqual(True, issubclass(int, (int, (float, int)))) self.assertEqual(True, issubclass(str, (str, (Child, str)))) + def test_tuple_subclass_methods_not_called(self): + # gh-83731: make sure isinstance() reads the items of a tuple + # subclass directly, without calling its __iter__(), + # __getitem__() or __len__(). + class T(tuple): + def __iter__(self): + raise AssertionError('__iter__ called') + def __getitem__(self, index): + raise AssertionError('__getitem__ called') + def __len__(self): + raise AssertionError('__len__ called') + + self.assertTrue(isinstance(1, T((int,)))) + def test_subclass_recursion_limit(self): # make sure that issubclass raises RecursionError before the C stack is # blown