typing_inspect
typing_inspect copied to clipboard
Add a simple is_type(t) method
It should determine if t is meaningful to use in an isinstance check.
We've got some code in 3.6 that was simply doing:
isinstance(t, type)
for this purpose. It worked. typing.List as well as all your basic types passed correctly.
It blows up in 3.7 onwards because the type of typing.List and friends has changed from type to typing._GenericAlias or typing._SpecialGenericAlias.
Not having a simple way to answer the question of is something is a type that is meaningfully usable for isinstance checks from other things is frustrating. (I'd argue that such a basic thing should even go in the Python stdlib, but this module may be the best playground for that to start in)
Yeah, this would be a reasonable thing to have. I will not have time to add it myself (and polish possible corner cases), but PRs are welcome.
I currently use isinstance(obj, type) or isinstance(obj, typing_inspect.typingGenericAlias).