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 type support #1283

Closed
qgallouedec opened this issue Sep 5, 2022 · 6 comments
Closed

Self type support #1283

qgallouedec opened this issue Sep 5, 2022 · 6 comments
Assignees
Labels
cat: generics Generic, TypeVar, containers enhancement

Comments

@qgallouedec
Copy link

It would be nice to support the Self type in the future. Is there a plan in this direction?

PEP 673

Code example

foo.py:

from typing_extensions import Self

class A:
    def func(self) -> Self:
        return self
$ pytype foo.py
[...]
File "/Users/quentingallouedec/foo.py", line 1, in <module>: typing_extensions.Self not supported yet [not-supported-yet]
@rchen152
Copy link
Contributor

rchen152 commented Sep 8, 2022

Generally, we add support for new typing features a few months after adding support for the corresponding Python version. Since Self is in 3.11, which we haven't started seriously looking at yet, it'll probably be sometime next year before we support it.

rchen152 added a commit that referenced this issue Jun 15, 2023
* Adds (very) partial support for typing.Self.
* Adds a missing __buffer__ method to bytes.

For  #1283.

PiperOrigin-RevId: 540159072
rchen152 added a commit that referenced this issue Jun 15, 2023
* Supports Self in a container annotation.
* Supports Self in a function parameter annotation.
* Supports Self in a nested class.

For #1283.

PiperOrigin-RevId: 540443019
@eltoder
Copy link

eltoder commented Jul 10, 2023

Hi @rchen152! What is the status of Self type now? It looks like you added support for even some complex cases, but when I try the example from OP with pytype 2023.06.16, I still get

File "/home/eltoder/dev/scratch/self_type.py", line 1, in <module>: typing.Self not supported yet [not-supported-yet]
File "/home/eltoder/dev/scratch/self_type.py", line 5, in func: bad return type [bad-return-type]
           Expected: Self
  Actually returned: A

@rchen152
Copy link
Contributor

I added some support specifically for Self type in .pyi (stub) files, to help with updating the typeshed version that pytype uses. From a pytype user perspective, what I'm adding now probably isn't particularly useful, since it doesn't allow you to use Self in .py source files.

@eltoder
Copy link

eltoder commented Jul 11, 2023

Got it, thanks!

rchen152 added a commit that referenced this issue Oct 20, 2023
* Turns typing.Self into a TypeVar and annotates `self` with it when we detect
  that a method signature uses typing.Self.
* Renames some variables, attributes, and methods related to `self` annotations
  because the previous names were confusing me.
* Fixes a bug in overlay_utils.not_supported_yet that prevented it from
  converting an alias to a TypeVar to an abstract value.
* Fixes a bug in convert.py that caused it to report spurious recursion errors
  if conversion to an abstract value raises NotImplementedError.

For #1283.

PiperOrigin-RevId: 574945687
rchen152 added a commit that referenced this issue Oct 20, 2023
@rchen152 rchen152 self-assigned this Oct 20, 2023
rchen152 added a commit that referenced this issue Oct 24, 2023
* Adds `bound=<class>` to Self occurrences.
* Prints typing.Self as `from typing import Self` in pyi files rather than
  defining a new TypeVar named Self.

For #1283.

PiperOrigin-RevId: 575328499
rchen152 added a commit that referenced this issue Oct 24, 2023
…yet error.

All that's left to implement is use of Self in attribute annotations.

For #1283.

PiperOrigin-RevId: 575492431
rchen152 added a commit that referenced this issue Oct 25, 2023
@rchen152
Copy link
Contributor

Next week's pytype release will have Self support.

@tboddyspargo
Copy link

@rchen152 - I'm getting Cannot use 'typing.Self' outside of a class in with a situation approximately like this in python 3.11:

class A:
    def a(self, **kwargs) -> "Self":
        return self.__class__(**(self.__dict__ | kwargs))

class B(A):
    @overrides.override
    def a(self, **kwargs) -> "Self":
        self.super().a(**(dict(option="default") | kwargs))

Is this considered an inappropriate use of typing.Self, or does this indicate a gap in pytype's support for its use in the annotations of methods?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cat: generics Generic, TypeVar, containers enhancement
Projects
None yet
Development

No branches or pull requests

4 participants