pytype icon indicating copy to clipboard operation
pytype copied to clipboard

`Self` type support

Open qgallouedec opened this issue 3 years ago • 1 comments

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]

qgallouedec avatar Sep 05 '22 13:09 qgallouedec

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 avatar Sep 08 '22 00:09 rchen152

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

eltoder avatar Jul 10 '23 18:07 eltoder

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.

rchen152 avatar Jul 11 '23 17:07 rchen152

Got it, thanks!

eltoder avatar Jul 11 '23 17:07 eltoder

Next week's pytype release will have Self support.

rchen152 avatar Oct 26 '23 00:10 rchen152