[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

Dominik V. report at bugs.python.org
Thu Nov 12 16:25:08 EST 2020


Dominik V. <dominik.vilsmeier1123 at gmail.com> added the comment:

Thinking more about it, I came to realize that it's not the Union that sits at the root of this behavior, but rather the caching performed by generic types in general. So if we consider

```
L1 = List[Union[int, str]]
L2 = List[Union[str, int]]
```

then `get_args(L1)[0] is get_args(L2)[0]` and so `get_args` has no influence on the order of arguments of the Union objects (they are already the same for L1 and L2).

So I think it would be more accurate to add the following sentence instead:

> 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 <report at bugs.python.org>
<https://bugs.python.org/issue42317>
_______________________________________


More information about the Python-bugs-list mailing list