Thanks. With that advice, I suppose to answer the question myself, I would distinguish a Generic alias from non-generic aliases (like Literal and Union) with code like this:


def is_generic_alias(python_type: type) -> bool:
    with suppress(AttributeError):
        return typing.Generic in typing.get_origin(python_type).__bases__
    return False


On Thu, 2022-09-29 at 10:26 -0700, Jelle Zijlstra wrote:


El jue, 29 sept 2022 a las 10:10, Paul Bryan (<pbryan@anode.ca>) escribió:
I'm trying to understand why Literal["a", "b", "c"] results in an object derived from _GenericAlias. It's not using TypeVar substitutions, so it's not generic per se. Is it just to provide the origin and arguments semantics?


The internal structure of typing classes is byzantine and shouldn't be relied upon.
 

That said, would would be the best way to distinguish between a "real" generic alias from literals and unions (both of which derive from _GenericAlias)?


You can use typing.get_origin:

In [23]: typing.get_origin(typing.Literal[1, 2, 3]) is typing.Literal

Out[23]: True

 

_______________________________________________
Typing-sig mailing list -- typing-sig@python.org
To unsubscribe send an email to typing-sig-leave@python.org
https://mail.python.org/mailman3/lists/typing-sig.python.org/
Member address: jelle.zijlstra@gmail.com