Issue32752
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) * ![]() |
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) * ![]() |
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) * ![]() |
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) * ![]() |
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:57 | admin | set | github: 76933 |
| 2021-05-02 20:26:14 | JelleZijlstra | set | status: open -> closed nosy:
+ JelleZijlstra resolution: fixed |
| 2018-08-01 14:18:09 | levkivskyi | set | messages: + msg322865 |
| 2018-07-23 20:38:52 | gvanrossum | set | nosy:
- gvanrossum |
| 2018-07-23 17:13:20 | Jared Deckard | set | nosy:
+ Jared Deckard messages: + msg322234 |
| 2018-02-09 17:57:04 | levkivskyi | set | nosy:
+ gvanrossum, levkivskyi messages: + msg311901 |
| 2018-02-05 20:05:04 | matrixise | set | nosy:
+ matrixise messages: + msg311686 |
| 2018-02-02 22:59:06 | Paul Pinterits | create | |
