For some background on the removal of __instancecheck__, check the linked issues here:
Thanks for the reference (the most relevant discussion starts
here). That said, I think I totally disagree with the underlying philosophy of throwing away a useful and intuitive feature (having `is_instance(foo, Union[Bar, Baz])` just work as you'd naively expect) in the name of making sure that people
understand there's a distinction between types and classes.
This seems opposed to the "zen" of python that there should be exactly one obvious way to do it, since (1) there isn't a way to do it without a third party library, and (2) the obvious way to do it is with `isinstance` and `issubclass`. Indeed, the current implementation makes it somewhat nonobvious even how to implement this functionality yourself in a third-party library (see
this gist).
One of the first things I did when playing around with the
`typing` module was to fire up the REPL, and try runtime typechecks:
>>> from typing import *
>>> isinstance(0, Union[int, float])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/lucaswiman/.pyenv/versions/3.6/lib/python3.6/typing.py", line 767, in __instancecheck__
raise TypeError("Unions cannot be used with isinstance().")
TypeError: Unions cannot be used with isinstance().
I
think the natural reaction of naive users of the library is "That's annoying.
Why? What is this library good for?", not "Ah, I've sagely learned a
valuable lesson about the subtle-and-important-though-unmentioned
distinction between types and classes!" The restriction against runtime type checking makes `typing` pretty much only useful when used with the external library `mypy` (or when writing a library with the same purpose as `mypy`), which is a pretty unusual situation for a standard library module. Mark Shannon's example also specifically does not apply to the types I'm thinking of for the reasons I mentioned:
For example,
List[int] and List[str] and mutually incompatible types, yet
isinstance([], List[int]) and isinstance([], List[str))
both return true.
There is no corresponding objection for `Union`; I can't think of any* inconsistencies or runtime type changes that would result from defining `_Union.__instancecheck__` as `any(isinstance(obj, t) for t in self.__args__`. For `Tuple`, it's true that `()` would be an instance of `Tuple[X, ...]` for all types X. However, the objection for the `List` case (IIUC; extrapolating slightly) is that the type of the object could change depending on what's added to it. That's not true for tuples since they're immutable, so it's not inconsistent to say that `()` is an instance of `Tuple[int, ...]` and `Tuple[str, ...]`, it's just applying a sensible definition to the base case of an empty tuple.
That said, it sounds like the decision has already been made, and this would be quite useful functionality to have in some form. What do people think about implementing `__contains__` (for `__instancecheck__`) and `__lt__` (for `__subclasscheck__`) for these cases? Then there would still be a convenient syntax for doing runtime type checking/analysis, but wouldn't violate Mark Shannon's objections.
Best,
Lucas