Skip to content

Function "builtins.callable" is not valid as a type #10434

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

Closed
ramalho opened this issue May 6, 2021 · 3 comments
Closed

Function "builtins.callable" is not valid as a type #10434

ramalho opened this issue May 6, 2021 · 3 comments

Comments

@ramalho
Copy link

ramalho commented May 6, 2021

Bug Report

Mypy does not recognize the built-in function callable as a valid type for type hinting.

In the spirit of PEP 585 -- Type Hinting Generics In Standard Collections, it should.

I understand callable is not subscriptable, and I would file a bug in BPO if you agree this issue is worth fixing.

Nevertheless, I can't use callable even to typecheck very basic code like this the example below.

To Reproduce

  1. Create a module with this:
def f(g: callable) -> None:
    g()

f(f)
f('a')
  1. Type check it:
$ mypy callable_type.py 
callable_type.py:3: error: Function "builtins.callable" is not valid as a type
callable_type.py:3: note: Perhaps you need "Callable[...]" or a callback protocol?
callable_type.py:4: error: callable? not callable
Found 2 errors in 1 file (checked 1 source file)

Expected Behavior

From a user perspective, the distinction between callable and abc.Callable (or typing.Callable) is as arbitrary as the distinction between list and typing.List, which PEP 585 finally fixed.

I believe Mypy should accept callable in type hints, and that Python should make callable implement __class_getitem__. If it's not a class, it should become one (this has happened before in the history of Python; it's a non-breaking change if done correctly).

Actual Behavior

Mypy refuses to accept builtins.callable in a type hint.

Your Environment

  • Mypy version used: 0.818
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.9.4
  • Operating system and version: MacOS 10.14.6.
@ramalho ramalho added the bug mypy got something wrong label May 6, 2021
@JelleZijlstra
Copy link
Member

There is an existing issue about making callable generic at runtime: https://bugs.python.org/issue42102.

If we implemented your proposal, we'd also have to teach --disallow-any-generics to tell people to give type parameters to callable, and then they'd be back to using Callable. So we'd be introducing a new feature and simultaneously tell people to not use it. As such, I don't think it makes sense for mypy to accept callable as a type until callable[...] is accepted at runtime.

@ramalho
Copy link
Author

ramalho commented May 6, 2021

Your answer makes perfect sense, thanks @JelleZijlstra.

I did not know about https://bugs.python.org/issue42102 and I agree we should wait for it to be solved.

@hauntsaninja hauntsaninja added feature and removed bug mypy got something wrong labels May 11, 2021
CangyuanLi added a commit to CangyuanLi/pyproject that referenced this issue May 3, 2023
@hauntsaninja
Copy link
Collaborator

Yeah, this should be tracked at python/cpython#86268. I think currently, people don't really want to make this change.

@hauntsaninja hauntsaninja closed this as not planned Won't fix, can't repro, duplicate, stale Aug 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants