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

False positive for: list((1,2))+[3,] #487

Closed
colinRawlings opened this issue Oct 14, 2020 · 4 comments
Closed

False positive for: list((1,2))+[3,] #487

colinRawlings opened this issue Oct 14, 2020 · 4 comments
Labels
enhancement New feature or request fixed in next version (main) A fix has been implemented and will appear in an upcoming version

Comments

@colinRawlings
Copy link

Environment data

  • Language Server version: 2020.10.0
  • OS and version: Win 10 Build 20175
  • Python version (& distribution if applicable, e.g. Anaconda): Python.org 3.6.8

Expected behaviour

No red squiggle under the expression: list((1,2))+[3,]

Actual behaviour

Red squiggle with message:

 Operator "+" not supported for types "list[Literal[1, 2]]" and "List[int]"
 Operator "+" not supported for types "list[Literal[1, 2]]" and "List[int]"Pylance 
(reportGeneralTypeIssues)

Also not sure why the message is duplicated?

Logs

XXX

Code Snippet / Additional information

XXX
@colinRawlings colinRawlings changed the title False positive for: list((1,2))+[3,] False positive for: list((1,2))+[3,] Oct 14, 2020
@jakebailey
Copy link
Member

I think this is somewhat expected; the first list is inferred to be List[Literal[1, 2]] and that isn't compatible with the other type.

If you write the following, it type checks:

x: List[int] = list((1,2))
y = x + [3,]

@erictraut
Copy link
Contributor

@jakebailey, I'll take a deeper look at this one. The expression (1,2) is inferred to be Tuple[Literal[1], Literal[2]], and I think that's correct. But there's a question (not defined in any of the PEPs) about how a tuple with heterogeneous entry types should be translated into an Iterable. Pyright is currently unioning all of the individual types, which is why it results in an Iterable[Literal[1, 2]] in this case. But arguably, literals should be stripped during this conversion, resulting to Iterable[int]. That would then produce a List[int] when passed through the list constructor. I need to investigate more to see if this would break other things.

@jakebailey jakebailey added the needs investigation Could be an issue - needs investigation label Oct 14, 2020
@github-actions github-actions bot removed the triage label Oct 14, 2020
@erictraut
Copy link
Contributor

I've improved the bidirectional type inference for tuple expressions. The logic is now smarter about when to honor literals and when to strip them.

This will be included in the next release of pylance.

@erictraut erictraut added enhancement New feature or request fixed in next version (main) A fix has been implemented and will appear in an upcoming version and removed needs investigation Could be an issue - needs investigation labels Oct 16, 2020
@jakebailey
Copy link
Member

This issue has been fixed in version 2020.10.2, which we've just released. You can find the changelog here: https://github.com/microsoft/pylance-release/blob/master/CHANGELOG.md#2020102-21-october-2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request fixed in next version (main) A fix has been implemented and will appear in an upcoming version
Projects
None yet
Development

No branches or pull requests

3 participants