[issue42317] Docs of `typing.get_args`: Mention that due to caching of typing generics the order of arguments for Unions can be different from the one of the returned tuple
New submission from Dominik V.
from typing import List, Union, get_args get_args(get_args(List[Union[int, str]])[0]) (
, ) get_args(get_args(List[Union[str, int]])[0]) ( , )
This is because `List[Union[int, str]] is List[Union[str, int]]`.
I understand that caching is useful to reduce the memory footprint of type hints, so I suggest to update the documentation of `get_args`. At the moment it reads:
> For a typing object of the form X[Y, Z, ...] these functions return X and (Y, Z, ...).
This seems to imply that the returned objects are identical to the ones in the form `X[Y, Z, ...]`. However that's not the case:
```python
>>> U1 = Union[int, str]
>>> U2 = Union[str, int]
>>> get_args(List[U1])[0] is U1
True
>>> get_args(List[U2])[0] is U2
False
I'm not so much concerned about the identity, but the fact that a subsequent call to `get_args` on the Union returns a different type seems to be relevant. So I propose to add the following sentence to the `get_args` docs:
[...], it gets normalized to the original class. If `X` is a `Union`, the order of `(Y, Z, ...)` can be different from the one of the original arguments `[Y, Z, ...]`.
Or alternatively:
[...], it gets normalized to the original class. If `X` is a `Union`, the order of `(Y, Z, ...)` is arbitrary.
The second version is shorter but it's not completely accurate (since the order is actually not arbitrary).
----------
assignee: docs@python
components: Documentation
messages: 380699
nosy: Dominik V., docs@python
priority: normal
severity: normal
status: open
title: Docs of `typing.get_args`: Mention that due to caching of typing generics the order of arguments for Unions can be different from the one of the returned tuple
type: enhancement
versions: Python 3.9
_______________________________________
Python tracker
Ken Jin
get_args(Union[int, str]) [int, str]
get_args(Union[str, int]) [str, int]
Which is slightly inconsistent with its behavior when nested in List. I don't think there's an easy way to fix this without breaking the cache (and also it makes sense that Unions' args aren't order dependent). So I'm all for updating the docs with your addition (slightly edited):
If `X` is a `Union`, the order of `(Y, Z, ...)` may be different from the order of the original arguments `[Y, Z, ...]`.
----------
nosy: +gvanrossum, kj, levkivskyi
versions: +Python 3.10
_______________________________________
Python tracker
Guido van Rossum
Ken Jin
Change by Dominik V.
Dominik V.
If `X` is a generic type, the returned objects `(Y, Z, ...)` might not be identical to the ones used in the form `X[Y, Z, ...]` due to type caching.
Everything else follows from there (including flattening of nested Unions).
----------
_______________________________________
Python tracker
Guido van Rossum
miss-islington
Change by miss-islington
miss-islington
Guido van Rossum
participants (4)
-
Dominik V.
-
Guido van Rossum
-
Ken Jin
-
miss-islington