I have a list
structure as a flattened input to a API
(point_x, point_y, thing_a, thing_b, thing_c)
This real structure is a very long list of a many flattened objects. This example is a much simplified case.
I would like to make the types that create it clear in python
from typing import Tuple
point = (int, int)
thing = (int, float, str)
TypeA = Tuple[point + thing]
This is a valid python type:
typing.Tuple[int, int, int, float, str]
However, MyPy does not like it .
Invalid type alias: expression is not a valid type [valid-type]
The real structure I have is very complex and it's helpful for devs to see how the structure is created.
How can I do this properly without MyPy causing errors
Just as an addendum, the following is not a valid solution
from typing import Tuple
Tuple[
int, # point_x
int, # point_y
int, # thing_a
float, # thing_b
str # thing_c
]
You need to define "type-level tuples", using tuple[...]
, not (...)
.
point = tuple[int, int]
thing = tuple[int, float, str]
You can unpack them to define TypeA
:
TypeA = tuple[*point, *thing]
which can then be used as a type hint:
x: TypeA = (1, 2, 3, 4.5, "foo") # OK
y: TypeA = ("bar", 2, 3, 4.5, "foo") # Error