Description
Bug Report
I have a decorator factory function that takes a schema and returns a decorator that validates something against the provided schema and also passes in an argument to the wrapped function. Here's a simplified version of the code:
from typing import Any, Callable, Concatenate, ParamSpec, TypeVar
P = ParamSpec("P")
Schema = TypeVar("Schema")
Ret = TypeVar("Ret")
def validate(
schema: Schema,
) -> Callable[
[Callable[Concatenate[int, P], Ret]],
Callable[P, Ret],
]:
def decorator(
f: Callable[
Concatenate[int, P], Ret
]
) -> Callable[P, Ret]:
def wrap(*args: P.args, **kwargs: P.kwargs) -> Ret:
# for simplicity, omitting schema validation
# that would normally happen here.
additional_arg = 42 # also simplified
return f(additional_arg, *args, **kwargs)
return wrap
return decorator
With mypy 1.1.1, the type annotations are fine. But with the recently released mypy 1.2.0, I get this error:
error: Incompatible return value type (got "Callable[[Arg(Callable[[int, **P], Ret], 'f')], Callable[P, Ret]]", expected "Callable[[Callable[[int, **P], Ret]], Callable[P, Ret]]") [return-value]
To Reproduce
Run the following with mypy 1.2.0 (example returns an error) and mypy 1.1.1 (no error):
- Playground URL: https://mypy-play.net/?mypy=1.2.0&python=3.11&gist=c8cc63e9ae1cc5b82fc0b613a26aa302
- Gist URL: https://gist.github.com/mypy-play/c8cc63e9ae1cc5b82fc0b613a26aa302
Expected Behavior
I'd expect mypy to not return an error unless there is a legitimate issue with the types in this code (if that's the case, please let me know!).
Moreover, the actual type in the error message doesn't seem to be a valid type (specifically Arg(Callable[[int, **P], Ret], 'f')
in this example).
Actual Behavior
error: Incompatible return value type (got "Callable[[Arg(Callable[[int, **P], Ret], 'f')], Callable[P, Ret]]", expected "Callable[[Callable[[int, **P], Ret]], Callable[P, Ret]]") [return-value]
Your Environment
- Mypy version used: 1.2.0
- Mypy command-line flags: none
- Mypy configuration options from
mypy.ini
(and other config files): n/a - Python version used: 3.11