On Aug 30, 2019, at 02:09, Philippe Prados <philippe.prados@gmail.com> wrote:
Why not extend isInstance to :
isinstance(object:Any,classinfo: Iterable | Union) ?
How would you pass a single type? And once you allow a single type, you shouldn’t have to do anything special to allow a Union, because a Union is already a type. And there’s a good reason isinstance only takes tuples of types, not arbitrary iterables: because a type can be iterable (e.g., an Enum contains all of its members), so it would make single types ambiguous. Just like str.endswith can’t take a string or iterable (strings contain their characters), so it gets the same solution used there and dozens of other places: tuples are special-cased. If we already have | (you’re using it in your annotation), we don’t have a problem to solve in the first place. The problem only comes up if we allow sets of types as shorthand for unions in annotations instead. And the only problem is that sets as shorthand don’t match the “tuples are special” rule in isinstance and elsewhere, so that rule has to change to something like “tuples and sets (and frozensets?) are special”. I don’t think we need (or want) any more complicated change than that.