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

Argument type is partially unknown for map #1400

Closed
DanielPechersky opened this issue Jun 5, 2021 · 5 comments
Closed

Argument type is partially unknown for map #1400

DanielPechersky opened this issue Jun 5, 2021 · 5 comments
Labels
bug Something isn't working fixed in next version (main) A fix has been implemented and will appear in an upcoming version

Comments

@DanielPechersky
Copy link

Environment data

  • Language Server version: 2021.6.0
  • OS and version: darwin x64
  • Python version (and distribution if applicable, e.g. Anaconda): python3.10.0b2
  • python.analysis.indexing: undefined
  • python.analysis.typeCheckingMode: strict

Expected behaviour

No errors, type checker should know that map(list, list[list[X]]) -> list[list[X]]

Actual behaviour

Argument type is partially unknown for map(list, lst)

Code Snippet / Additional information

lst: list[list[int]] = [[1,2,3], [4]]
lst2: list[list[int]] = list(map(list, lst))
#                            ^^^^^^^^^^^^^^
# Argument type is partially unknown
#   Argument corresponds to parameter "iterable" in function "__init__"
#   Argument type is "map[list[Unknown]]" Pylance(reportUnknownArgumentType)
@github-actions github-actions bot added the triage label Jun 5, 2021
@erictraut
Copy link
Contributor

erictraut commented Jun 5, 2021

This behavior is a known limitation of bidirectional type inference. In the first case (with variable lst), the type engine is able to use the type annotation on the LHS (left-hand side) of the assignment to inform the inferred type of the RHS expression. In the second case (with variable lst2), this cannot be done because bidirectional type inference cannot be applied to argument expressions used within a call expression. Therefore, the inferred type of map(list, lst) is list[unknown].

In cases like this, you have several options:

  1. Explicitly specialize the list argument to the map call:
lst2 = list(map(list[int], lst))
  1. Use a temporary variable with an annotation:
lst2_temp: map[list[int]] = map(list, lst)
lst2 = list(lst2_temp)
  1. Use a cast (not a great option because it overrides type checker logic):
lst2 = list(cast(map[list[int]], map(list, lst)))
  1. Use a "# type: ignore" comment to suppress the error (also not a great option).
lst2: list[list[int]] = list(map(list, lst)) # type: ignore

@DanielPechersky
Copy link
Author

Alright, that sounds good. Specializing list sounds like the best approach here.

@DanielPechersky
Copy link
Author

DanielPechersky commented Jun 6, 2021

Actually, why doesn't specialization work for this case?

lst = [[1,2,3],[4]]
tup: tuple[tuple[int, ...], ...] = tuple(map(tuple[int, ...], lst))
#                                        ^^^^^^^^^^^^^^^^^^^^^^^^^
# No overloads for "__init__" match the provided arguments
#   Argument types: (Type[tuple[int, ...]], list[list[int]]) Pylance(reportGeneralTypeIssues)

@erictraut
Copy link
Contributor

That's a bug! Thanks for reporting it. This will be fixed in the next release.

@erictraut erictraut added bug Something isn't working fixed in next version (main) A fix has been implemented and will appear in an upcoming version and removed triage labels Jun 6, 2021
@jakebailey
Copy link
Member

This issue has been fixed in version 2021.6.1, which we've just released. You can find the changelog here: https://github.com/microsoft/pylance-release/blob/main/CHANGELOG.md#202161-9-june-2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working 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