Starred expressions
In most cases, mypy can infer the type of starred expressions from theright-hand side of an assignment, but not always:
- a, *bs = 1, 2, 3 # OK
- p, q, *rs = 1, 2 # Error: Type of rs cannot be inferred
On first line, the type of bs
is inferred to beList[int]
. However, on the second line, mypy cannot infer the typeof rs
, because there is no right-hand side value for rs
toinfer the type from. In cases like these, the starred expression needsto be annotated with a starred type:
- p, q, *rs = 1, 2 # type: int, int, List[int]
Here, the type of rs
is set to List[int]
.