https://github.com/python/cpython/commit/21a36aa79ca0b2af49995fa1b9216b01ef98f195
commit: 21a36aa79ca0b2af49995fa1b9216b01ef98f195
branch: main
author: Vasiliy Kiryanov <[email protected]>
committer: methane <[email protected]>
date: 2026-10-16T13:16:11+09:00
summary:

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__().

files:
M Lib/test/test_isinstance.py

diff --git a/Lib/test/test_isinstance.py b/Lib/test/test_isinstance.py
index 16d879a3831bc6..a3f4659d871919 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,))))
+
     @support.skip_if_huge_c_stack()
     @support.skip_wasi_stack_overflow()
     @support.skip_emscripten_stack_overflow()

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to