New submission from Paul Pinterits rawing7@gmail.com:
The documentation of the typing module explains how to instantiate generic types, but there is no information about how to extract the type arguments from a generic type.
Example:
list_of_ints = typing.List[int]
# how do we get <class 'int'> out of list_of_ints? list_of_ints.???
<class 'int'>
Through trial and error I've discovered list_of_ints.__args__, which *seems* to be what I'm looking for, but since it's never mentioned in the docs, it's unclear whether this __args__ attribute is an implementation detail or not.
Please document the official/intended way to extract type arguments from a Generic.
---------- assignee: docs@python components: Documentation messages: 311520 nosy: Paul Pinterits, docs@python priority: normal severity: normal status: open title: no information about accessing typing.Generic type arguments type: enhancement versions: Python 3.5, Python 3.6
_______________________________________ Python tracker report@bugs.python.org https://bugs.python.org/issue32752 _______________________________________
Stéphane Wirtel stephane@wirtel.be added the comment:
with the help command, you have a good documentation, help(list_of_ints), but you are right, there is no functions/methods for your need.
in the code of typing, there is a comment about __args__ but it is not used by the documentation.
---------- nosy: +matrixise
_______________________________________ Python tracker report@bugs.python.org https://bugs.python.org/issue32752 _______________________________________
Ivan Levkivskyi levkivskyi@gmail.com added the comment:
There is a third party library on PyPI called typing_inspect that provides thin wrappers around internal APIs to get lots of useful information about generics and other special types in typing.
If there will be more requests like this, then the most used functions from there might be moved to typing itself. Or is it already enough? (Especially in view of recent typing refactoring that simplified the internal APIs.)
---------- nosy: +gvanrossum, levkivskyi
_______________________________________ Python tracker report@bugs.python.org https://bugs.python.org/issue32752 _______________________________________
Jared Deckard jared@shademaps.com added the comment:
typing_inspect is now filled with python version checks for 3.7. The implementation got significantly more complex when differentiating generics at runtime.
3.6 was undocumented, but the OO API was intuitive:
T = typing.Union[int, float] assert T.__class__ == typing.Union assert T.__args__ == (int, float)
3.7 is less convenient and less consistent:
T = typing.Union[int, float] assert T.__class__ == typing._GenericAlias assert T.__origin__ == typing.Union L = typing.List[int] assert L.__class__ == typing._GenericAlias assert L.__origin__ == list
---------- nosy: +Jared Deckard
_______________________________________ Python tracker report@bugs.python.org https://bugs.python.org/issue32752 _______________________________________
Change by Guido van Rossum guido@python.org:
---------- nosy: -gvanrossum
_______________________________________ Python tracker report@bugs.python.org https://bugs.python.org/issue32752 _______________________________________
Ivan Levkivskyi levkivskyi@gmail.com added the comment:
3.7 is less convenient and less consistent
Taking into account that internal API is something one should never use, I don't think these terms apply here. Anyway, we will provide some helpers for external use in one of the next releases.
----------
_______________________________________ Python tracker report@bugs.python.org https://bugs.python.org/issue32752 _______________________________________
Jelle Zijlstra jelle.zijlstra@gmail.com added the comment:
We have typing.get_args() as of 3.8.
---------- nosy: +Jelle Zijlstra resolution: -> fixed stage: -> resolved status: open -> closed
_______________________________________ Python tracker report@bugs.python.org https://bugs.python.org/issue32752 _______________________________________