Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
intgr opened this issue Oct 11, 2023 · 2 comments
Labels
bug mypy got something wrong topic-paramspec PEP 612, ParamSpec, Concatenate

Comments

@intgr
Copy link
Contributor

intgr commented Oct 11, 2023

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
@intgr intgr added the bug mypy got something wrong label Oct 11, 2023
@AlexWaygood AlexWaygood added the topic-paramspec PEP 612, ParamSpec, Concatenate label Oct 11, 2023
@ilevkivskyi
Copy link
Member

I can't reproduce this on master, I guess it was fixed by #15913 (behavior of Callable[[Concatenate[X, Y, ...]], Z] actually was not clearly specified in the PEP, but examples like this show it is better to handle it similar to plain ...).

This is a relatively significant PR, so it is unlikely to be backported to 1.6 branch, OTOH 1.7 will be released in 3 weeks that will include the fix.

@intgr
Copy link
Contributor Author

intgr commented Oct 13, 2023

Confirmed fixed in latest mypy_mypyc-wheels build.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-paramspec PEP 612, ParamSpec, Concatenate
Projects
None yet
Development

No branches or pull requests

3 participants