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, protocol and classmethod #13911

Closed
mehdigmira opened this issue Oct 16, 2022 · 4 comments · Fixed by #15903
Closed

Paramspec, protocol and classmethod #13911

mehdigmira opened this issue Oct 16, 2022 · 4 comments · Fixed by #15903
Labels
bug mypy got something wrong topic-paramspec PEP 612, ParamSpec, Concatenate

Comments

@mehdigmira
Copy link

Bug Report

Mypy doesn't infer the right type in the following example.

To Reproduce

https://gist.github.com/94a0c6c7b64ea26e03b9a44ae460c6cb

Expected Behavior

mypy infers Type of "Add.async_func()" is "(x: int, y: int) -> int" as pyright does

Actual Behavior

mypy infers Revealed type is "def (*<nothing>, **<nothing>) -> builtins.int"

Your Environment

  • Mypy version used: 0.981
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.10.7
@mehdigmira mehdigmira added the bug mypy got something wrong label Oct 16, 2022
@AlexWaygood AlexWaygood added the topic-paramspec PEP 612, ParamSpec, Concatenate label Mar 3, 2023
@AlexWaygood
Copy link
Member

So that others don't have to click through, here's the snippet:

from typing import TypeVar, Protocol, Type, Callable
from typing_extensions import ParamSpec

ReturnT = TypeVar("ReturnT", covariant=True)
P = ParamSpec("P")

class AsyncP(Protocol[P]):

    def __init__(self, *args: P.args, **kwargs: P.kwargs) -> None:
        ...

    
class Async:

    @classmethod
    def async_func(cls: Type[AsyncP[P]]) -> Callable[P, int]:
        ...


class Add(Async):

    def __init__(self, x: int, y: int) -> None:
        self.x = x
        self.y = y

        
reveal_type(Add.async_func())

And here's a link to mypy playground: https://mypy-play.net/?mypy=latest&python=3.11&gist=34604ea95d10df9b6bcf2de46e8383a2

@ilevkivskyi
Copy link
Member

Note that part of the problem is that you are using __init__ in protocol, which is very special, see #15844. The other part of the problem will be fixed by #15903

ilevkivskyi added a commit that referenced this issue Aug 19, 2023
Fixes #14968
Fixes #13911

The fix is simple, as I predicted on Discord, we simply should use
`get_all_type_vars()` instead of `get_type_vars()` (that specifically
returns only `TypeVarType`). I also use this opportunity to tidy-up code
in `bind_self()`, it should be now more readable, and much faster
(especially when compiled with mypyc).

cc @A5rocks

---------

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
@hauntsaninja
Copy link
Collaborator

@mehdigmira is the project you were running into this on open source?

@mehdigmira
Copy link
Author

No the project is not open source

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.

4 participants