Hi, today I found a surprising fact that types.GenericAlias and typing._GenericAlias are different. Seems user defined generic uses typing._GenericAlias and builtin types uses types.GenericAlias
>>> type(list[int])
<class 'types.GenericAlias'>
>>> class Foo[T]: pass
...
>>> type(Foo[int])
<class 'typing._GenericAlias'>
>>> type(list[int]) is type(Foo[int])
False
Is this intentional or some sort of bug? What is the correct way to check if an object is an generic alias?
What’s goal of knowing that? What makes Foo[int] and list[int] related?
If your goal is whether type has type arguments/generic base you can use get_origin/get_args. If get_origin returns None then it’s not a subscripted generic type.
I am working on a stub generator, that involves evaluating generic types (replace TypeVar with concrete types). In this case builtin types are not very different from user-defined generics.
get_origin will do the job, but it not as readable or type-checker-friendly as a match statement in my case like:
match t:
case TypeAliasTypes():
...
case GenericAlias():
...