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