On Aug 30, 2019, at 11:26, Philippe Prados <philippe.prados@gmail.com> wrote:

May be, with 
isinstance(object:Any,classinfo: Tuple | Union) ?
it's possible to accept

isinstance(anUnion, [Union]) # Force tuple for Union
isinstance(aTuple, [Tuple] # Force Tuple
isinstance(aStr, int | str)

Correct ?

No.

Currently, isinstance takes a type, or a tuple of types. You’re changing it to take a tuple of anything, or a value of any Union of any types. What is that supposed to do?

I think you’re mixing up types and values here (it may be easier to think of List instead of Union here: [1, 2, 3] is a List, because it’s a List[int], a list of bits; List[int] is not a List, because it’s not a list of anything, it’s just a single value). But even beyond that, I don’t understand why you want to stop isinstance from taking individual types. Or why you think you need to add anything for Union types. Union types are already types. So isinstance already accepts them. 

Meanwhile, [Union] and [Tuple] are not tuples, they’re lists. So, they don’t work today, and they wouldn’t work with your change.

If you pass a single-value tuple (Tuple,), that does work today, but I’m not sure why you’d bother given that it means the exact same thing as just passing Tuple. Anyway, either way, that already works.

As for int|str, as you said at the start of this thread, that just returns Union[int, str]. It’s the exact same value, so it has the exact same type, so you don’t need to add anything new to anyone’s signature to handle it the same.

Taking a step back, I suspect your real issue is with this:

    >>> isinstance(2, Union[int|str])
    TypeError: Subscripted generics cannot be used with class an instance checks

But this has nothing to do with the signature of isinstance. That Union is a type, and isinstance correctly accepts it as a type, and correctly follows the protocol, which includes calling its __subclasscheck__ method. It’s that method, Union.__subclasscheck__, that’s preventing you from making this test, as you can see from the traceback, and it’s even explaining why.

And this isn’t a bug; it’s implementing exactly what the design and docs say it should. You’re not supposed to use instantiated generic types in isinstance because that’s almost always a sign of confusion between static and dynamic types, not a useful thing to do.

You could argue that Union is a special case, unlike most of the other generics, and it actually is useful more often than a mistake, and therefore it should have different rules. But you have to make that argument, and then come up with those different rules, and then convince people they’re better, and then implement them in the subclasscheck method. Changing the signature of isinstance is irrelevant. And that’s all orthogonal to, and almost completely unrelated to, adding |.