You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This describes an issue where a generic decorator that returns a generic sub-type of a "callable" (using __call__ and ParamSpec) cannot be applied to a generic function. In the example below, Callable[_P, _R] -> Traceable[_P, _R] works, but Callable[_P, _R] -> Decorated[_P, _R] does not. It seems to work if either the decorated function is not generic (E.G. radius() instead of apply() in the example), or if the return type of the decorator is a super-type of its argument (E.G. Traceable instead of Decorated).
I encountered a very similar problem when trying to overload a decorator to support it with kwargs or without. I think it all boils down to mypy resolving T in a return type Callable[[Callable[..., T]], Callable[..., T]] to Never unless there are other indicators for how to resolve T in any args/kwargs. It would be amazing if mypy could support it.
This describes an issue where a generic decorator that returns a generic sub-type of a "callable" (using
__call__
andParamSpec
) cannot be applied to a generic function. In the example below,Callable[_P, _R] -> Traceable[_P, _R]
works, butCallable[_P, _R] -> Decorated[_P, _R]
does not. It seems to work if either the decorated function is not generic (E.G.radius()
instead ofapply()
in the example), or if the return type of the decorator is a super-type of its argument (E.G.Traceable
instead ofDecorated
).Relevant closed issues
To Reproduce
https://mypy-play.net/?mypy=latest&python=3.12&gist=a8f681e6c14ec013bf3ae56c81fe94b2
Expected Behavior
Expected variables transferred from input generic callable to returned generic callable, even if the return is not a super-type of the input.
Actual Behavior
ParamSpec variables are not used to parameterize the returned generic if it is not a super-type of the input.
Your Environment
python -m mypy -v typehint_decorator.py
The text was updated successfully, but these errors were encountered: