This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: isinstance accepts subtypes of tuples as second argument
Type: behavior Stage:
Components: Versions: Python 3.9
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: ewjoachim, p-ganssle, serhiy.storchaka, vstinner
Priority: normal Keywords:

Created on 2020-02-04 18:30 by ewjoachim, last changed 2022-04-11 14:59 by admin.

Files
File name Uploaded Description Edit
isinstance_iter.py vstinner, 2020-02-04 21:31
Messages (4)
msg361362 - (view) Author: Joachim Jablon (ewjoachim) * Date: 2020-02-04 18:29
(Not really sure it is a bug, but better informed people might find it worthy still)

isinstance can accept, as second argument, a type or a potentially nested tuple of types. Only tuples are accepted, as opposed to generic iterables. The reasoning behind using a tuple was recently added through a small refactoring from Victor Stinner:
https://github.com/python/cpython/commit/850a4bd839ca11b59439e21dda2a3ebe917a9a16
The idea being that it's impossible to make a self referencing tuple nest, and thus the function, which is recursive, doesn't have to deal with infinite recursion.

It's possible to use a tuple subclass, though, and while it doesn't break the function because it reads , the tuple is not explored through the __iter__ interface:

>>> class T(tuple):
...     def __iter__(self):
...             yield self
... 
>>> isinstance(3, T())
False

This is the expected result if checking what the tuple contains, but not if iterating the tuple. For me, there's nothing absolutely wrong with the current behaviour, but it feels like we're walking on a fine line, and if for any reason, the isinstance tuple iteration were to start using __iter__ in the future, this example may crash. Solutions could be handling any iterable but explicitely checking for recursion or, as suggested by Victor Stinner, forbidding subclasses of tuple.

Guido van Rossum suggested opening an issue so here it is.

A link to the discussion that prompted this:
https://twitter.com/VictorStinner/status/1224744606421655554
msg361363 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2020-02-04 18:59
I do not understand what the problem is.

If the isinstance tuple iteration were to start using __iter__ in the future, it should start to handle all corner cases. But what is wrong now?
msg361365 - (view) Author: Paul Ganssle (p-ganssle) * (Python committer) Date: 2020-02-04 19:49
Serhiy: I think at least a test for this particular corner case should be added, so that no implementations of `isinstance` that use the CPython test suite hit an infinite recursion in that event, I guess?

Though I think it's maybe an open question as to what the correct behavior is. Should we throw on any tuple subclass because there's no reason to support tuple subclasses? Should we switch to using __iter__ when it's defined because there are other cases where the custom behavior of the subclass is defined by its __iter__? Should we make it a guarantee that __iter__ is *never* called?

I can't really think of a reason why defining __iter__ on a tuple subclass would be anything other than a weird hack, so I would probably say either ban tuple subclasses or add a test like so:

def testIsinstanceIterNeverCalled(self):
    """Guarantee that __iter__ is never called when isinstance is invoked"""
    class NoIterTuple(tuple):
        def __iter__(self):  # pragma: nocover
            raise NotImplemented("Cannot call __iter__ on this.")

    self.assertTrue(isinstance(1, NoIterTuple((int,))))
msg361371 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2020-02-04 21:31
Joachim:
> It's possible to use a tuple subclass, though, and while it doesn't break the function because it reads , the tuple is not explored through the __iter__ interface: (...)

I don't think that the example from the first message is an issue in isinstance(). Defining __iter__() does not override __getitem__() nor __len__():
---
class T(tuple):
    def __iter__(self):
        print("__iter__")
        yield self

inst = T(("a", "b"))
print(len(inst))
print(inst[0])
print(inst[1])
---

Output:
---
2
a
b
---

But isinstance() doesn't call overriden __getitem__() and __len__() methods, only tuple.__getitem__() and tuple.__len__() original methods.


Joachim:
>  Solutions could be handling any iterable but explicitely checking for recursion or, as suggested by Victor Stinner, forbidding subclasses of tuple.

isinstance() should use PyTuple_CheckExact(): only accept the exact tuple type, and reject any other type.

We can start by deprecating tuple subclasses in Python 3.9.


Paul:
> Should we switch to using __iter__ when it's defined because there are other cases where the custom behavior of the subclass is defined by its __iter__?

I just restored an old comment from Guido van Rossum which was lost in a previous refactoring, commit 850a4bd839ca11b59439e21dda2a3ebe917a9a16:

    if (PyTuple_Check(cls)) {
        /* Not a general sequence -- that opens up the road to
           recursion and stack overflow. */
       ...
    }

--

By the way, nested tuples are accepted:

>>> isinstance(1, ((((int,),),),))
True

I am -0 on nested tuples. The implementation is not so crazy (simple recursive code), so it is maybe ok to keep it.
History
Date User Action Args
2022-04-11 14:59:26adminsetgithub: 83731
2020-02-04 21:31:43vstinnersetfiles: + isinstance_iter.py

messages: + msg361371
2020-02-04 21:15:35vstinnersetnosy: + vstinner
2020-02-04 19:49:31p-gansslesetnosy: + p-ganssle
messages: + msg361365
2020-02-04 18:59:21serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg361363
2020-02-04 18:30:00ewjoachimcreate