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

self argument is not assignable to variable typed as Self #17760

Closed
DetachHead opened this issue Sep 12, 2024 · 5 comments
Closed

self argument is not assignable to variable typed as Self #17760

DetachHead opened this issue Sep 12, 2024 · 5 comments
Labels
bug mypy got something wrong

Comments

@DetachHead
Copy link
Contributor

from typing import Self

class Foo:
    def foo(self) -> None:
        asdf: Self = self
main.py:5: error: Incompatible types in assignment (expression has type "Foo", variable has type "Self")  [assignment]
Found 1 error in 1 file (checked 1 source file)

playground

@DetachHead DetachHead added the bug mypy got something wrong label Sep 12, 2024
@JamesParrott
Copy link

JamesParrott commented Sep 12, 2024

I agree this could be improved. FYI, typing self: Self works:

playground

@brianschubert
Copy link
Collaborator

I took a stab at making self have an implicit type of Self instead of Foo to see what would happen (for posterity sake, the relevant logic is here).

It turns out there's a test against that:

[case testTypingSelfAttrOldVsNewStyle]
from typing import Self, TypeVar
T = TypeVar("T", bound='C')
class C:
x: Self
def foo(self: T) -> T:
return self.x
def bar(self: T) -> T:
self.x = self
return self
def baz(self: Self) -> None:
self.x = self
def bad(self) -> None:
# This is unfortunate, but required by PEP 484
self.x = self # E: Incompatible types in assignment (expression has type "C", variable has type "Self")

I'm not sure I understand the comment about this being a PEP 484 requirement. It seems that pyright gets away making the type of an unannotated self compatible/identical to Self: Pyright playground

@hauntsaninja
Copy link
Collaborator

There's a sentence in PEP 484 about not needing to annotate self and treat it as the type of the containing class. Looks like that sentence is in the current spec too and we should maybe change that: https://typing.readthedocs.io/en/latest/spec/annotations.html#annotating-instance-and-class-methods

I don't think that's too much of an issue though, I seem to remember folks were concerned about performance. Let me dig up a reference...

@hauntsaninja
Copy link
Collaborator

Yeah, see #14075 (closing this as a duplicate of that)

@hauntsaninja hauntsaninja closed this as not planned Won't fix, can't repro, duplicate, stale Sep 27, 2024
@hauntsaninja
Copy link
Collaborator

Let's also change the spec to be clearer about this python/typing#1860

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

4 participants