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

Passing self to a generic function that accepts Self #17723

Open
NeilGirdhar opened this issue Aug 30, 2024 · 1 comment
Open

Passing self to a generic function that accepts Self #17723

NeilGirdhar opened this issue Aug 30, 2024 · 1 comment
Labels
bug mypy got something wrong

Comments

@NeilGirdhar
Copy link
Contributor

NeilGirdhar commented Aug 30, 2024

from typing import Generic, Self, TypeVar

T = TypeVar('T')


class X(Generic[T]):
    def f(self, t: T) -> None:
        pass


class Y:
    def g(self) -> None:
        X[Self]().f(self)  # error: Argument 1 to "f" of "X" has incompatible type "Y"; expected "Self"  [arg-type]
@NeilGirdhar NeilGirdhar added the bug mypy got something wrong label Aug 30, 2024
@NeilGirdhar NeilGirdhar changed the title Self narrowed early when passed as a type parameter Passing self as a parameter to a generic function that accepts Self seems to fail Aug 30, 2024
@NeilGirdhar NeilGirdhar changed the title Passing self as a parameter to a generic function that accepts Self seems to fail Passing self to a generic function that accepts Self Aug 30, 2024
@bmerry
Copy link
Contributor

bmerry commented Sep 5, 2024

I've just run into a similar issue (although using a Protocol rather than a Generic). It seems that declaring g as

    def g(self: Self) -> None:

makes things pass, which surprised me since I thought self: Self was implicit (PEP 673 declares it to be "harmless").

bmerry added a commit to ska-sa/aiokatcp that referenced this issue Sep 5, 2024
Clients that don't keep up with sensor updates will now be disconnected,
instead of causing the server to use an ever-increasing amount of RAM.

This required some unintuitive changes to keep mypy happy, due to
python/mypy#17723.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

2 participants