typing_inspect icon indicating copy to clipboard operation
typing_inspect copied to clipboard

Add a simple is_type(t) method

Open gpshead opened this issue 4 years ago • 2 comments

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)

gpshead avatar Feb 13 '21 00:02 gpshead

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.

ilevkivskyi avatar Feb 18 '21 11:02 ilevkivskyi

I currently use isinstance(obj, type) or isinstance(obj, typing_inspect.typingGenericAlias).

mitar avatar Sep 27 '21 13:09 mitar