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

mypy --strict has misleading suggestion for Callable #11757

Closed
frerikandriessen opened this issue Dec 15, 2021 · 2 comments · Fixed by #12518
Closed

mypy --strict has misleading suggestion for Callable #11757

frerikandriessen opened this issue Dec 15, 2021 · 2 comments · Fixed by #12518
Labels
bug mypy got something wrong good-first-issue topic-configuration Configuration files and flags topic-error-reporting How we report errors topic-usability

Comments

@frerikandriessen
Copy link
Contributor

Bug Report

It's not a big issue, not sure if you should even qualify it as a bug but I fought it fitted better than other categories.
When in strict mode, it gives a suggestion that is actually not valid in strict mode.

To Reproduce

Running mypy --strict on this code

from typing import Callable

def my_function(callback: Callable[...]) -> None:
    callback()

gives the following error message:

$ mypy callable_test.py --strict
callable_test.py:3: error: Please use "Callable[[<parameters>], <return type>]" or "Callable"        
Found 1 error in 1 file (checked 1 source file)

However, changing the code to use the second suggestion of using Callable:

from typing import Callable

def my_function(callback: Callable) -> None:
    callback()

gives a new error message:

$ mypy callable_test.py --strict
callable_test.py:3: error: Missing type parameters for generic type "Callable"
Found 1 error in 1 file (checked 1 source file)

Running mypy in non-strict mode does not give this error. But if it is at all possible it would be nice to remove the second suggestion in strict mode (or turn it into Callable[..., Any]).

I got confused (didn't spot that I was missing an Any return value in my Callable[...] annotation) and kept switching back and forth wondering why the suggestion was not working. Which admittedly is my mistake. But it did bring to light for me that the suggestion is a bit misleading in this context.

I don't know if this is easily done or not, if it's a relatively minor fix I'd be willing to give it a crack.

Additional info:

$ mypy --version
mypy 0.902
$ python --version
Python 3.8.10
@frerikandriessen frerikandriessen added the bug mypy got something wrong label Dec 15, 2021
@khalidgt95
Copy link

Encountering the same issue as mentioned by @frerikandriessen.

My versions of Python and mypy:

mypy 0.931
Python 3.7.1

@AlexWaygood
Copy link
Member

The line of code creating the error message is here:

self.fail('Please use "Callable[[<parameters>], <return type>]" or "Callable"', t)

I think we just need to make the error message slightly different if --disallow-any-generics is set to True (this option is automatically set to True if mypy is being run with --strict). To find out if --disallow-any-generics is set to True, you should be able to check whether self.options.disallow_any_generics is True or False.

hauntsaninja pushed a commit that referenced this issue May 7, 2022
Adds a more precise error message for a Callable annotation, if the
option --disallow-any-generics (part of --strict mode) has been used.
The suggestion in the old message was incorrect in this scenario.

Fixes #11757
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong good-first-issue topic-configuration Configuration files and flags topic-error-reporting How we report errors topic-usability
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants