def is_generic_alias(python_type: type) -> bool:
with suppress(AttributeError):
return typing.Generic in typing.get_origin(python_type).__bases__
return False
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