Closed
Description
Bug Report
I have a decorator-like function that takes a Callable[Concatenate[...], ...]
as an argument and returns a wrapped callable with the same signature.
After upgrading from mypy 1.5.1 to 1.6.0, a wrapped function is no longer considered compatble when it should be. My understanding is that Callable[..., Any]
should be compatible with every possible callable, but it fails with:
main.py:24: error: Argument 1 to "wrap_func" has incompatible type "Callable[[str], bool]"; expected "Callable[[str, VarArg(Any), KwArg(Any)], Any]" [arg-type]
To Reproduce
- Mypy 1.6.0 ❌ https://mypy-play.net/?mypy=1.6.0&python=3.11&gist=6af0ba1e82c661584eee28e19f02880b
- Mypy 1.5.1 ✅ https://mypy-play.net/?mypy=1.5.1&python=3.11&gist=6af0ba1e82c661584eee28e19f02880b
from typing import Concatenate, ParamSpec, TypeVar, reveal_type, Any, Callable
P = ParamSpec("P")
RT = TypeVar("RT")
def wrap_func(wrapped_func: Callable[Concatenate[str, P], RT]) -> Callable[Concatenate[str, P], RT]:
return wrapped_func
def func(value: str, /) -> bool:
return bool(value)
wrapped0 = wrap_func(func)
reveal_type(wrapped0) # N: Revealed type is "def (builtins.str) -> builtins.bool"
wrapped1: Callable[[str], Any] = wrap_func(func) # ✅
wrapped2: Callable[[str], object] = wrap_func(func) # ✅
wrapped3: Callable[[str], int] = wrap_func(func) # ✅
wrapped4: Callable[[str], bool] = wrap_func(func) # ✅
wrapped5: Callable = wrap_func(func) # ❌ expected "Callable[[str, VarArg(Any), KwArg(Any)], Any]"
wrapped6: Callable[..., Any] = wrap_func(func) # ❌ expected "Callable[[str, VarArg(Any), KwArg(Any)], Any]"
wrapped7: Callable[..., object] = wrap_func(func) # ❌ expected "Callable[[str, VarArg(Any), KwArg(Any)], bool]"
wrapped8: Callable[..., int] = wrap_func(func) # ❌ expected "Callable[[str, VarArg(Any), KwArg(Any)], int]"
wrapped9: Callable[..., bool] = wrap_func(func) # ❌ expected "Callable[[str, VarArg(Any), KwArg(Any)], bool]"
Actual Behavior
main.py:24: error: Argument 1 to "wrap_func" has incompatible type "Callable[[str], bool]"; expected "Callable[[str, VarArg(Any), KwArg(Any)], Any]" [arg-type]
main.py:25: error: Argument 1 to "wrap_func" has incompatible type "Callable[[str], bool]"; expected "Callable[[str, VarArg(Any), KwArg(Any)], Any]" [arg-type]
main.py:26: error: Argument 1 to "wrap_func" has incompatible type "Callable[[str], bool]"; expected "Callable[[str, VarArg(Any), KwArg(Any)], bool]" [arg-type]
main.py:27: error: Argument 1 to "wrap_func" has incompatible type "Callable[[str], bool]"; expected "Callable[[str, VarArg(Any), KwArg(Any)], int]" [arg-type]
main.py:28: error: Argument 1 to "wrap_func" has incompatible type "Callable[[str], bool]"; expected "Callable[[str, VarArg(Any), KwArg(Any)], bool]" [arg-type]
Your Environment
- Mypy version used: 1.6.0
- Mypy command-line flags: -
- Python version used: 3.11.6