New submission from Joachim Jablon <ewjoac...@gmail.com>:

(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

----------
messages: 361362
nosy: ewjoachim
priority: normal
severity: normal
status: open
title: isinstance accepts subtypes of tuples as second argument
type: behavior
versions: Python 3.9

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue39550>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to