This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: no information about accessing typing.Generic type arguments
Type: enhancement Stage: resolved
Components: Documentation Versions: Python 3.6, Python 3.5
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: docs@python Nosy List: Jared Deckard, JelleZijlstra, Paul Pinterits, docs@python, levkivskyi, matrixise
Priority: normal Keywords:

Created on 2018-02-02 22:59 by Paul Pinterits, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (6)
msg311520 - (view) Author: Paul Pinterits (Paul Pinterits) Date: 2018-02-02 22:59
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.
msg311686 - (view) Author: Stéphane Wirtel (matrixise) * (Python committer) Date: 2018-02-05 20:05
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.
msg311901 - (view) Author: Ivan Levkivskyi (levkivskyi) * (Python committer) Date: 2018-02-09 17:57
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.)
msg322234 - (view) Author: Jared Deckard (Jared Deckard) Date: 2018-07-23 17:13
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
msg322865 - (view) Author: Ivan Levkivskyi (levkivskyi) * (Python committer) Date: 2018-08-01 14:18
> 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.
msg392723 - (view) Author: Jelle Zijlstra (JelleZijlstra) * (Python committer) Date: 2021-05-02 20:26
We have typing.get_args() as of 3.8.
History
Date User Action Args
2022-04-11 14:58:57adminsetgithub: 76933
2021-05-02 20:26:14JelleZijlstrasetstatus: open -> closed

nosy: + JelleZijlstra
messages: + msg392723

resolution: fixed
stage: resolved
2018-08-01 14:18:09levkivskyisetmessages: + msg322865
2018-07-23 20:38:52gvanrossumsetnosy: - gvanrossum
2018-07-23 17:13:20Jared Deckardsetnosy: + Jared Deckard
messages: + msg322234
2018-02-09 17:57:04levkivskyisetnosy: + gvanrossum, levkivskyi
messages: + msg311901
2018-02-05 20:05:04matrixisesetnosy: + matrixise
messages: + msg311686
2018-02-02 22:59:06Paul Pinteritscreate