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

ParamSpec and protocols #13694

Closed
mehdigmira opened this issue Sep 20, 2022 · 3 comments
Closed

ParamSpec and protocols #13694

mehdigmira opened this issue Sep 20, 2022 · 3 comments
Labels
bug mypy got something wrong topic-paramspec PEP 612, ParamSpec, Concatenate topic-protocols

Comments

@mehdigmira
Copy link

Bug Report

ParamSpec feature doesn't seem to work well when used with Protocol.

To Reproduce

from typing import Any, ParamSpec, Protocol

P = ParamSpec("P")


class Runnable(Protocol[P]):
    def run(self, *args: P.args, **kwargs: P.kwargs) -> Any:
        ...


def foo(func: Runnable) -> int:
    ...


class A:
    def run(self, i: int) -> Any:
        ...


foo(A())

Expected Behavior

IMO, mypy shouldn't report any bug, do you agree ?

Actual Behavior

mypy logs an error, which means mypy doesn't recognize A as implementing the Runnableprotocol (which it does)

commands/typing_debug.py:20: error: Argument 1 to "foo" has incompatible type "A"; expected "Runnable[Any]"
commands/typing_debug.py:20: note: Following member(s) of "A" have conflicts:
commands/typing_debug.py:20: note:     Expected:
commands/typing_debug.py:20: note:         def run(self, *args: Any, **kwargs: Any) -> Any
commands/typing_debug.py:20: note:     Got:
commands/typing_debug.py:20: note:         def run(self, i: int) -> Any

However if foo is defined as

def foo(func: Runnable[int]) -> int:
    ...

mypy doesn't report any error.

Your Environment

  • Mypy version used: 0.971
  • Mypy command-line flags: no flag
  • Mypy configuration options from mypy.ini (and other config files): no config file
  • Python version used: 3.10.2
  • Operating system and version: MacOS 12.6
@mehdigmira mehdigmira added the bug mypy got something wrong label Sep 20, 2022
@AlexWaygood AlexWaygood added topic-protocols topic-paramspec PEP 612, ParamSpec, Concatenate labels Sep 21, 2022
@mehdigmira
Copy link
Author

If a maintainer can acknowledge this is a bug, and give some hints on where to start digging, I'm willing to give fixing this a shot :)

@erictraut
Copy link

I'm able to type check the code above without errors using the latest published version of mypy (0.981), so I think this bug has been fixed already.

@hauntsaninja
Copy link
Collaborator

Yes, I fixed this in #11203. Let me know if you see anything bad with ParamSpec + Protocols on mypy 0.981

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 topic-protocols
Projects
None yet
Development

No branches or pull requests

4 participants