Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mypy cannot infer type of [[], [1]] #2255

Closed
squiddy opened this issue Oct 14, 2016 · 5 comments
Closed

Mypy cannot infer type of [[], [1]] #2255

squiddy opened this issue Oct 14, 2016 · 5 comments
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-2-low

Comments

@squiddy
Copy link

squiddy commented Oct 14, 2016

mypy 0.4.5, CPython 3.4.4

Running the code with mypy results in an error:

x = [[], [1]]
for b in x:
    reveal_type(b) # error: Revealed type is 'builtins.object*'
    b + [1]  # error: Unsupported left operand type for + ("object")

This might be me misunderstanding the type system, but I would have expected mypy to ask for type annotations if it can't infer the type, e.g.:

error: Need type annotation for variable

I'm also wondering why it infers an object, when it could (should?) be a list instead.

@gvanrossum
Copy link
Member

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.

@rwbarton
Copy link
Contributor

The workaround is to add an explicit type signature to x:

x = [[], [1]] # type: List[List[int]]

@gnprice gnprice changed the title Incorrect infered type in for loop Incorrect inferred type in for loop Oct 20, 2016
@gvanrossum gvanrossum added this to the Future milestone Oct 20, 2016
@ddfisher ddfisher changed the title Incorrect inferred type in for loop Mypy cannot infer type of [[], [1]] Oct 20, 2016
@gvanrossum
Copy link
Member

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 :-).

@gvanrossum gvanrossum removed this from the Future milestone Mar 29, 2017
@JukkaL JukkaL added the false-positive mypy gave an error on correct code label May 18, 2018
@Alberth289346
Copy link

Looks like the same issue as #5045

@ilevkivskyi
Copy link
Member

Duplicate of #230

@ilevkivskyi ilevkivskyi marked this as a duplicate of #230 Sep 14, 2023
@ilevkivskyi ilevkivskyi closed this as not planned Won't fix, can't repro, duplicate, stale Sep 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-2-low
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants