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

Inference failure with Protocol and ParamSpec #15734

Closed
mtomassoli opened this issue Jul 21, 2023 · 0 comments · Fixed by #15913
Closed

Inference failure with Protocol and ParamSpec #15734

mtomassoli opened this issue Jul 21, 2023 · 0 comments · Fixed by #15913
Labels
bug mypy got something wrong topic-paramspec PEP 612, ParamSpec, Concatenate

Comments

@mtomassoli
Copy link

mtomassoli commented Jul 21, 2023

To Reproduce

mypy Playground

from typing import Protocol, ParamSpec, Callable, Concatenate

P = ParamSpec('P')

class FuncType(Protocol[P]):
    def __call__(self, x: int, s: str,
                 *args: P.args, **kw_args: P.kwargs) -> str: ...

def forwarder1(fp: FuncType[P], *args: P.args, **kw_args: P.kwargs) -> str:
    return fp(0, '', *args, **kw_args)

def forwarder2(fp: Callable[Concatenate[int, str, P], str],
               *args: P.args, **kw_args: P.kwargs) -> str:
    return fp(0, '', *args, **kw_args)

def my_f(x: int, s: str, d: float) -> str:
    return f"{x}, {s}, {d}"

# error: Argument 1 to "forwarder1" has incompatible type "Callable[[int, str, float], str]";
#   expected "FuncType[<nothing>]"  [arg-type]
# error: Argument 2 to "forwarder1" has incompatible type "float";
#   expected <nothing>  [arg-type]
forwarder1(my_f, 1.2)

forwarder2(my_f, 1.2)

Expected Behavior

No error found.

Actual Behavior

Argument 1 to "forwarder1" has incompatible type "Callable[[int, str, float], str]"; expected "FuncType[]" [arg-type]
Argument 2 to "forwarder1" has incompatible type "float"; expected [arg-type]

@mtomassoli mtomassoli added the bug mypy got something wrong label Jul 21, 2023
@ilevkivskyi ilevkivskyi added the topic-paramspec PEP 612, ParamSpec, Concatenate label Aug 18, 2023
hauntsaninja pushed a commit that referenced this issue Sep 14, 2023
Fixes #15734
Fixes #15188
Fixes #14321
Fixes #13107 (plain Callable was
already working, this fixes the protocol example)
Fixes #16058

It looks like treating trivial suffixes (especially for erased
callables) as "whatever works" is a right thing, because it reflects the
whole idea of why we normally check subtyping with respect to an e.g.
erased type. As you can see this fixes a bunch of issues. Note it was
necessary to make couple more tweaks to make everything work smoothly:
* Adjust self-type erasure level in `checker.py` to match other places.
* Explicitly allow `Callable` as a `self`/`cls` annotation (actually I
am not sure we need to keep this check at all, since we now have good
inference for self-types, and we check they are safe either at
definition site or at call site).
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

Successfully merging a pull request may close this issue.

2 participants