You should be able to use the __parameters__, __origin__ and __args__ dunders to introspect things like list[Thing[T]][int] fully.

On Thu, Mar 16, 2023 at 9:09 AM Adrian Garcia Badaracco <adrian@adriangb.com> wrote:
>  The safest option would be to follow convention and just return a
> GenericAlias that captures whatever is passed into `__class_getitem__`.

The main problem with `GenericAlias` is that it's this undocumented class where it seems like effort has been taken to keep it as a private implementation detail. Libraries like Pydantic (and it sounds like numpy?) want to keep track of what generic parameters were substituted and use that information when instantiated (or more ideally whenever there are no more type vars left and one could build a "concrete" type). But GenericAlias doesn't expose that functionality, despite internally tracking all of this. It's only interaction with the original class is to forward any calls from `GenericAlias.__call__(*args, **kwargs)` to `cls(*args, **kwargs)`.

Could we devise a way for `GenericAlias` to be more useful to libraries that do runtime type checking? Or if the desire is to keep GenericAlias could we devise an API for something that can be returned from `__class_getitem__` that is compatible with being used in `List[Thing[T]][int]`? The simplest form I can think of for the latter is commenting out that line of code and allowing `__parameters__` to be collected from arbitrary objects, but I imagine there's a reason that check is there / something would break.
_______________________________________________
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: guido@python.org


--
--Guido van Rossum (python.org/~guido)
Pronouns: he/him (why is my pronoun here?)