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

Created on 2018-02-02 22:59 by Paul Pinterits, last changed 2018-08-01 14:18 by levkivskyi.

Messages (5)
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.
History
Date User Action Args
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