python / python/mypy

Passing Callable to higher-order functions expecting Type[Callable] fails

Open
#15,593 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

Mypy reports that higher-order functions with parameters of type Type[Callable] - or any subscripted variant - can't receive Callable (with compatible subscripts where needed) as arguments.

To Reproduce

Playground link (with more detail and further test cases):
https://mypy-play.net/?mypy=1.4.1&python=3.11&gist=bd6418069e6a7800eb0e2067829a2c90

def test_fn(_: Type[Callable]) -> None:
    """A higher order fn accepting some TYPE, instances of which are callable"""
    pass

test_fn(Callable)  # FAILS
test_fn(Callable[[int], None])  # FAILS

TestCase: TypeAlias = Callable[[int], None]
test_fn(TestCase)  # FAILS

Expected Behavior

From https://docs.python.org/3/library/typing.html#type-of-class-objects

A variable annotated with type[C] (or typing.Type[C]) may accept values that are classes themselves – specifically, it will accept the class object of C.

So, passing Callable to a function expecting a Type[Callable], either directly or via a TypeAlias, should work.

Actual Behavior

When a subscripted Callable expression is passed directly in the function call, mypy reports:

error: Argument 1 to "test_fn" has incompatible type "object"; expected "type[Callable[..., Any]]"  [arg-type]

When bare Callable is passed in-place, or when any Callable expression is passed via a variable, with or without a TypeAlias annotation, this becomes:

error: Argument 1 to "test_fn" has incompatible type "<typing special form>"; expected "type[Callable[..., Any]]"  [arg-type]

Your Environment

  • Mypy version used: Playground 1.4.1 (originally discovered in 1.3.0)
  • Mypy command-line flags: Playground defaults
  • Mypy configuration options from mypy.ini (and other config files): Playground defaults
  • Python version used: 3.11

Other Notes

Pylance/Pyright appears to be fine with this.

The actual use-case I have is as a (generic) factory function which, given the type of some callable, will return an appropriate version of a corresponding generic class, which manages registering and triggering callbacks of the original callable type. The constructed class preserves and forwards the function signature annotations from the callable onto its own methods, via a ParamSpec and return-type TypeVar. The function receives a type, rather than a callable instance, as when defining the callback interface I wouldn't typically have a function of the correct type to hand.

As the playgound link shows, I'm also intending that the factory fn support receiving callable Protocol classes, which while more flexible than collections.abc.Callable (kwargs!) are also much more verbose. Mypy seems happy with these.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the three failing cases in the linked mypy playground, including bare, subscripted, and aliased Callable forms. The issue names no source file or test; done means compatible Callable expressions are accepted when passed to Type[Callable] parameters without the reported arg-type errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.