Skip to content

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

@ramalho

Description

@ramalho

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.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions