Skip to content

Decorator factory function typing  #15037

Description

@ssamojl

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:

fromtypingimportAny, Callable, Concatenate, ParamSpec, TypeVarP=ParamSpec("P")
Schema=TypeVar("Schema") Ret=TypeVar("Ret") defvalidate(
schema: Schema,
) ->Callable[
[Callable[Concatenate[int, P], Ret]],
Callable[P, Ret],
]:
defdecorator(
f: Callable[
Concatenate[int, P], Ret
]
) ->Callable[P, Ret]:
defwrap(*args: P.args, **kwargs: P.kwargs) ->Ret: # for simplicity, omitting schema validation # that would normally happen here.additional_arg=42# also simplifiedreturnf(additional_arg, *args, **kwargs)
returnwrapreturndecorator

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):

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-paramspecPEP 612, ParamSpec, Concatenate

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions