Skip to content

Better messages when type is incompatible due to invariance #3352

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

Closed
JukkaL opened this issue May 10, 2017 · 3 comments
Closed

Better messages when type is incompatible due to invariance #3352

JukkaL opened this issue May 10, 2017 · 3 comments

Comments

@JukkaL
Copy link
Collaborator

JukkaL commented May 10, 2017

Users are often confused by compatibility of invariant collection types. Here is an example from #3351:

from typing import List, Union

def mean(numbers: List[Union[int, float]]) -> float:
    return sum(numbers) / len(numbers)

some_numbers = [1, 2, 3, 4]
mean(some_numbers)  # Argument 1 to "mean" has incompatible type List[int]; 
                    # expected List[Union[int, float]]

It might be helpful to add a note with more details, since the message doesn't really provide enough context to start googling for help. Here is one idea:

program.py:23:note: "List" is invariant -- see <link to docs>
program.py:23:note: Maybe you can use "Sequence" as the target type, which is covariant?
@ilevkivskyi
Copy link
Member

Isn't this a duplicate of #1115?

@quartox
Copy link
Contributor

quartox commented May 22, 2017

I will start looking at this.

ilevkivskyi pushed a commit that referenced this issue Aug 19, 2017
Fixes #1115 and #3352 by providing a link to docs and a suggestion.
This currently only takes care of 'List' and 'Dict', which are the most typical for this kind of errors.
@ilevkivskyi
Copy link
Member

This is now fixed by #3411

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

No branches or pull requests

3 participants