0

For instance, what is a good syntax for a type like that:

list[int | list[int | ...]]

Basically, for a list of ints that could be of any (strictly positive) dimension.

LoneCodeRanger
  • 413
  • 6
  • 18

1 Answers1

0

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 ints 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'
LoneCodeRanger
  • 413
  • 6
  • 18