python / mypy

Optional static typing for Python
https://www.mypy-lang.org/
Other
18.28k stars 2.8k forks source link

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

Open Darren-GZ opened 1 year ago

Darren-GZ commented 1 year ago

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

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.

erictraut commented 1 year ago

FWIW, I've modified pyright to disallow type[Callable] and emit an error if this is used. I don't think this is a valid annotation because Callable is not instantiable at runtime through a constructor call. If you agree with this logic, then you may want to add a similar error in mypy for this condition.