For instance, what is a good syntax for a type like that:
list[int | list[int | ...]]
Basically, for a list
of int
s that could be of any (strictly positive) dimension.
For instance, what is a good syntax for a type like that:
list[int | list[int | ...]]
Basically, for a list
of int
s that could be of any (strictly positive) dimension.
I was able to find a solution, by following the logic in this answer (as suggested by @juanpa.arrivillaga), and using a forward reference.
The type (T
) for an n-dimensional list
of int
s could be noted as:
import typing
T = list[typing.Union[int, 'T']]
N.B.: whereas typing.Union(T1, T2)
can also be written as T1 | T2
, it doesn't work in the context of this answer (as of Python 3.9):
>>> T = list[int | 'T']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for |: 'type' and 'str'