Skip to content

mypy --strict has misleading suggestion for Callable #11757

Closed

Description

@frerikandriessen

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

Activity

  1. khalidgt95 commented on Mar 25, 2022

    @khalidgt95

    Encountering the same issue as mentioned by @frerikandriessen.

    My versions of Python and mypy:

    mypy 0.931
    Python 3.7.1
    
  2. AlexWaygood commented on Mar 31, 2022

    @AlexWaygood
    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.

  3. added a commit that references this issue on May 7, 2022
    269adee
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions