Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions pep-0673.rst
Original file line number Diff line number Diff line change
Expand Up @@ -605,11 +605,6 @@ The following uses of ``Self`` are accepted:
# Accepted (treated as an @property returning the Callable type)
bar: Callable[[Self], int] = foo

TupleSelf = Tuple[Self, Self]
class Alias:
def return_tuple(self) -> TupleSelf:
return (self, self)

class HasNestedFunction:
x: int = 42

Expand Down Expand Up @@ -660,6 +655,20 @@ The following uses of ``Self`` are rejected.

class Baz(Foo[Self]): ... # Rejected

We reject type aliases containing ``Self``. Supporting ``Self``
outside class definitions can require a lot of special-handling in
type checkers. Given that it also goes against the rest of the PEP to
use ``Self`` outside a class definition, we believe the added
convenience of aliases is not worth it:

::

TupleSelf = Tuple[Self, Self] # Rejected

class Alias:
def return_tuple(self) -> TupleSelf: # Rejected
return (self, self)

Note that we reject ``Self`` in staticmethods. ``Self`` does not add much
value since there is no ``self`` or ``cls`` to return. The only possible use
cases would be to return a parameter itself or some element from a container
Expand Down Expand Up @@ -781,8 +790,8 @@ Other languages have similar ways to express the type of the enclosing class:

Thanks to the following people for their feedback on the PEP:

Jia Chen, Rebecca Chen, Sergei Lebedev, Kaylynn Morgan, Tuomas Suutari, Alex
Waygood, Shannon Zhu, and Никита Соболев
Jia Chen, Rebecca Chen, Sergei Lebedev, Kaylynn Morgan, Tuomas
Suutari, Eric Traut, Alex Waygood, Shannon Zhu, and Никита Соболев

Copyright
=========
Expand Down