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

Can't infer type of input (dict) and think it is an "object". #4588

Closed
hvnsweeting opened this issue Feb 19, 2018 · 2 comments
Closed

Can't infer type of input (dict) and think it is an "object". #4588

hvnsweeting opened this issue Feb 19, 2018 · 2 comments

Comments

@hvnsweeting
Copy link
Contributor

  • Are you reporting a bug, or opening a feature request?
    Bug

  • Please insert below the code you are checking with mypy,
    or a mock-up repro if the source is private. We would appreciate
    if you try to simplify your case to a minimal repro.

from typing import Dict

idata = {'a': 1, 'prices': {'first_50': 1230, 'from_51_to_100': 1530, 'above_100': 1786}}

def dictin(names):
    # type: (Dict) -> int
    return 5


if __name__ == "__main__":
    dictin(idata['prices'])
  • What is the actual behavior/output?
$ mypy dictin.py
dictin.py:11: error: Argument 1 to "dictin" has incompatible type "object"; expected "Dict[Any, Any]"
  • What is the behavior/output you expect?
    Know that I passed a correct dict, thus no error.

If I remove the key:value 'a': 1, the check passed without any error.

  • What are the versions of mypy and Python you are using?
    Do you see the same issue after installing mypy from Git master?
    Latest one
 mypy --version
mypy 0.570-dev-b0af822a79465a93d55e67b5aea74551a63c08f4
@ilevkivskyi
Copy link
Member

This is not really a bug, mypy can't infer such complex types. For this case you probably want to use TypedDict, see http://mypy.readthedocs.io/en/latest/kinds_of_types.html#typeddict. This is the only way to let mypy know that a given key has a specific value type.

@JukkaL
Copy link
Collaborator

JukkaL commented Feb 19, 2018

This is a usability issue, however, and comes up pretty frequently. We have an existing issue (#3816) with an idea about how to improve this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants