Skip to content

[regression] Function wrapped with ParamSpec[Concatenate[]] is not compatible with Callable[..., Any] #16247

Closed
@intgr

Description

@intgr

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

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

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