Open Darren-GZ opened 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.
Bug Report
Mypy reports that higher-order functions with parameters of type
Type[Callable]
- or any subscripted variant - can't receiveCallable
(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
Expected Behavior
From https://docs.python.org/3/library/typing.html#type-of-class-objects
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:
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:Your Environment
mypy.ini
(and other config files): Playground defaultsOther 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.