Using Python 3.9, I'm used to declaring a tuple as t: tuple[A, B]
. However, I now have a list[int]
, which is converted to a tuple
. Obviously, the size of the list is only known at runtime. How do I declare a typing hint for the variable storing tuple(some_list))
?
Asked
Active
Viewed 94 times
0

Abhijit Sarkar
- 21,927
- 20
- 110
- 219
-
3`tuple[int, ...]` – juanpa.arrivillaga Sep 14 '21 at 03:51
-
I closed as a duplicate – juanpa.arrivillaga Sep 14 '21 at 03:53
-
You cannot type hint a heterogenous, arbitrarily sized tuple. In general, arbitrarily sized tuples are a strange thing. They are meant to be used as [product types](https://en.wikipedia.org/wiki/Product_type) – juanpa.arrivillaga Sep 14 '21 at 03:53
-
One thing you *can* do is use something like `tuple[Union[str, int, whatever], ...]` but that might now have the exact semantics you are hoping for – juanpa.arrivillaga Sep 14 '21 at 03:57
-
Another option is `tuple[Any, ...]` which will also probably not have the exact semantics you're hoping for; anything will pass... – Jiří Baum Sep 14 '21 at 05:44