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

Confusing message if callable is incompatible due to argument name #4530

Open
JukkaL opened this issue Feb 1, 2018 · 5 comments
Open

Confusing message if callable is incompatible due to argument name #4530

JukkaL opened this issue Feb 1, 2018 · 5 comments
Labels
bug mypy got something wrong priority-0-high topic-calls Function calls, *args, **kwargs, defaults topic-error-reporting How we report errors topic-usability

Comments

@JukkaL
Copy link
Collaborator

JukkaL commented Feb 1, 2018

Mypy generates a confusing error for this program:

from typing import Callable, Union
from mypy_extensions import Arg

def f(x: Callable[[Arg(int, 'x')], None]) -> None: pass

y: Callable[[Union[int, str]], None]
f(y)  # error

Here is the output:

t.py:7: error: Argument 1 to "f" has incompatible type "Callable[[Union[int, str]], None]"; expected "Callable[[int], None]"

The actual error is the missing argument name in the type of y, but the error message gives no hint about this.

@JukkaL
Copy link
Collaborator Author

JukkaL commented Feb 21, 2018

Added high priority since this is very confusing and we just received another report about this.

@ilevkivskyi
Copy link
Member

Another similar example with even more confusing error message was found by @msullivan:

from typing import Optional, List, Tuple, Dict, Callable

def foo(x: int) -> None: pass
def bar(x: int) -> None: pass

funcs = [foo, bar]
reveal_type(funcs)

def oops(x: List[Callable[[int], None]]) -> None:
    reveal_type(x)

oops(funcs)

produces the error message
Argument 1 to "oops" has incompatible type "List[Callable[[int], None]]"; expected "List[Callable[[int], None]]", followed by some advice about covariance.

@ilevkivskyi
Copy link
Member

We should probably schedule this one for next sprint usability bugs squashing week. This happened again, it can very challenging with more complex signatures.

@Michael0x2a
Copy link
Collaborator

We also run into similar issues with sufficiently complex overload signatures -- for example, I ended up being confused by this sort of thing in #5235.

FWIW, one solution I was sort of tossing around was to modify or refactor subtypes.is_callable_compatible so it returns the reason why two callables are incompatible, rather then just a bool. The code doing the checking could then either pass that info into the error handler, or the error handling logic could re-run that check to extract the reason.

@ilevkivskyi
Copy link
Member

Note that the error can be even more cryptic if type inference fails because of name mismatch, see #6928 for example.

@AlexWaygood AlexWaygood added topic-calls Function calls, *args, **kwargs, defaults topic-error-reporting How we report errors labels Apr 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong priority-0-high topic-calls Function calls, *args, **kwargs, defaults topic-error-reporting How we report errors topic-usability
Projects
None yet
Development

No branches or pull requests

4 participants