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

Incorrect error message when passing a dict to a function #1716

Closed
SizzlingVortex opened this issue Jun 16, 2016 · 1 comment
Closed

Incorrect error message when passing a dict to a function #1716

SizzlingVortex opened this issue Jun 16, 2016 · 1 comment

Comments

@SizzlingVortex
Copy link

SizzlingVortex commented Jun 16, 2016

from typing import Mapping


def func(results: Mapping[str, str]) -> None:
    pass


if __name__ == "__main__":
    # Passing an int here (even though all values should be strings) is intentional
    func({"1":"1", "2":2})

Running the above code through mypy gives me the following incorrect error message:

error: List item 1 has incompatible type "Tuple[str, int]"

Strangely enough, the next section of code (which is nearly the same as the above code) produces a better error message:

from typing import Mapping


def func(results: Mapping[str, str]) -> None:
    pass


if __name__ == "__main__":
    # Using an int here (even though all values should be strings) is intentional
    my_dict = {"1":"1", "2":2}
    func(my_dict)

Running this code through mypy gives the following (better) error message:

error: Argument 1 to "func" has incompatible type Dict[str, object]; expected Mapping[str, str]

My Specs:
Python 3.5.1
mypy-lang 0.4.2

@JukkaL
Copy link
Collaborator

JukkaL commented Jun 16, 2016

This is a duplicate of an existing issue (#304).

The examples are a little different from mypy's point of view. In the the first case mypy tries to infer a type for the dictionary expression based on the context where it is used (Mapping[str, str]), and the values don't conform to the context. In the second case mypy infers a type for the dictionary based on no context, so it can give the dictionary are wide enough type. However, this turns out to be incompatible with the function argument later on.

@JukkaL JukkaL closed this as completed Jun 16, 2016
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

2 participants