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

Dictionary unpacking of a TypedDict yields incorrect Dict[str, object] type #1328

Closed
juanpablos opened this issue May 22, 2021 · 2 comments
Closed
Labels
enhancement New feature or request fixed in next version (main) A fix has been implemented and will appear in an upcoming version

Comments

@juanpablos
Copy link

Environment data

  • Language Server version: 2021.5.3
  • OS and version: Windows 10
  • Python version (& distribution if applicable, e.g. Anaconda): Python 3.8.5

Snippet

from typing import TypedDict


class Config(TypedDict):
    arg1: int
    arg2: str


def function(arg1: int, arg2: str):
    ...

my_configs: Config = {"arg1": 10, "arg2": "something"}

####
function(**my_configs)
configs_copy = {**my_configs}
values = my_configs.values()

Expected behaviour

  • function(**my_configs) should pass the type check
  • configs_copy = {**my_configs} should either have an inferred type of Dict[str, Union[int, str]] or Dict[str, Any], or at least Dict[str, Unknown]
  • my_configs.values() should, same as above, have at least ValuesView[Unknown] inferred type

Actual behaviour

  • function(**my_configs) fails to type check with the following message:
(variable) my_configs: Config
Argument of type "object" cannot be assigned to parameter "arg1" of type "int" in function "function"
  "object" is incompatible with "int"PylancereportGeneralTypeIssues
  • configs_copy = {**my_configs} has an inferred type of Dict[str, object]
  • my_configs.values() is inferred as ValuesView[object]

Other info

This type of TypedDict unpacking was working before, though not inferring the types of the keys of the dictionary. However, we can agree that unpacking a TypedDict should not yield an object type.
Work on this bug could end up fulfilling (at least partially) this feature request I did some time ago #374

@erictraut
Copy link
Contributor

Thanks for the feature request.

I've added support in the function call logic to support unpacked TypedDict arguments. It now properly handles both allowed and disallowed cases. This will be included in the next release.

I haven't changed the logic for unpacked TypedDict expressions in other contexts because I'm not yet convinced that Dict[str, object] is the wrong answer here. This is consistent with the behavior dictated by the "dict" class definition in typeshed, and it's consistent with mypy's behavior.

If you want the unpacked form of your TypedDict to be typed as Dict[str, Any], you can use a type annotation, like this:

configs_copy: Dict[str, Any] = {**my_configs}

@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 triage labels May 23, 2021
@jakebailey
Copy link
Member

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

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