You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
mypy --strict has misleading suggestion for Callable #11757
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.
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.
Activity
khalidgt95 commentedon Mar 25, 2022
Encountering the same issue as mentioned by @frerikandriessen.
My versions of Python and mypy:
AlexWaygood commentedon Mar 31, 2022
The line of code creating the error message is here:
mypy/mypy/typeanal.py
Line 816 in 544d21a
I think we just need to make the error message slightly different if
--disallow-any-genericsis set toTrue(this option is automatically set toTrueif mypy is being run with--strict). To find out if--disallow-any-genericsis set toTrue, you should be able to check whetherself.options.disallow_any_genericsisTrueorFalse.Add more precise error message for Callable annotation (#12518)