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

Misleading issue text when return type doesn't match type annotation #14773

Open
tusharsadhwani opened this issue Feb 24, 2023 · 1 comment
Open
Labels
topic-type-context Type context / bidirectional inference topic-usability

Comments

@tusharsadhwani
Copy link
Contributor

tusharsadhwani commented Feb 24, 2023

Bug Report

When you return a function call like return list(...), return dict(...), and the type doesn't match the return annotation, the error message says that list(...) is not valid code.

To Reproduce

def foo() -> list[int]:
    tup = ("a", "b", "c")
    return list(tup)

Expected Behavior

I was expecting mypy to tell me that the type annotation doesn't match the return type.

Instead it says that the arguments to list() are wrong, i.e. list doesn't take this type.

Like so:

$ mypy asd.py
asd.py:3: error: Incompatible return value type (got "List[str]", expected "List[int]")  [return-value]
Found 1 error in 1 file (checked 1 source file)

Actual Behavior

$ mypy asd.py 
asd.py:3: error: Argument 1 to "list" has incompatible type "Tuple[str, str, str]"; expected "Iterable[int]"  [arg-type]
Found 1 error in 1 file (checked 1 source file)

I think this is misleading.

Your Environment

  • Mypy version used: v1.0.1
  • Mypy command-line flags: N/A
  • Mypy configuration options from mypy.ini (and other config files): N/A
  • Python version used: 3.10.9
@tusharsadhwani tusharsadhwani added the bug mypy got something wrong label Feb 24, 2023
@JelleZijlstra JelleZijlstra added the topic-type-context Type context / bidirectional inference label Feb 24, 2023
@JukkaL JukkaL added topic-usability and removed bug mypy got something wrong labels Apr 22, 2023
@JukkaL
Copy link
Collaborator

JukkaL commented Apr 22, 2023

Removing the bug label, since the error message is consistent with how type inference is designed to work in mypy, but I agree that this can be confusing and a generating a more useful error message would be nice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-type-context Type context / bidirectional inference topic-usability
Projects
None yet
Development

No branches or pull requests

3 participants