You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Thanks for the report. It's not directly related to the for loop, it's just that in this case the item type of the outer list is inferred as the join of (the types of) [] and [1] and for obscure reasons that becomes object instead of list. E.g.
a= [[], [1]]
reveal_type(a)
shows that the type of a is builtins.list[builtins.object*].
You're not getting the error Need type annotation for variable because that is only used for "partial types" (that's a mypy-internal term), and partial types are only inferred for variables directly assigned from None or an empty container. Here we've got something more complicated and it tries to directly infer a type for the outer list, and failing to notice the similarity between an empty list and a list of integers.
I think we can fix this though I'm not sure how important it is or how soon it'll happen.
Activity
gvanrossum commentedon Oct 14, 2016
Thanks for the report. It's not directly related to the
forloop, it's just that in this case the item type of the outer list is inferred as the join of (the types of)[]and[1]and for obscure reasons that becomes object instead of list. E.g.shows that the type of
aisbuiltins.list[builtins.object*].You're not getting the error
Need type annotation for variablebecause that is only used for "partial types" (that's a mypy-internal term), and partial types are only inferred for variables directly assigned fromNoneor an empty container. Here we've got something more complicated and it tries to directly infer a type for the outer list, and failing to notice the similarity between an empty list and a list of integers.I think we can fix this though I'm not sure how important it is or how soon it'll happen.
rwbarton commentedon Oct 15, 2016
The workaround is to add an explicit type signature to
x:[-]Incorrect infered type in for loop[/-][+]Incorrect inferred type in for loop[/+][-]Incorrect inferred type in for loop[/-][+]Mypy cannot infer type of `[[], [1]]`[/+]gvanrossum commentedon Oct 20, 2016
Since this can be worked around by adding annotations I don't consider this very urgent, and it's hard to fix (see the closed PR :-).
Alberth289346 commentedon May 15, 2020
Looks like the same issue as #5045
ilevkivskyi commentedon Sep 14, 2023
Duplicate of #230