-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Add support for Self type #14041
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
Add support for Self type #14041
Changes from 12 commits
b205241
5e5bd6f
7f91f3f
80a11f6
2eb0db1
535d936
ca7c7e8
2ee66ec
ccb74a7
504fe2c
1a99961
ce8d345
324eff2
d96cfdc
0b953cf
5829804
3ec47b9
24dd649
ac6234d
61c0589
cbd97b1
362d84a
a5740eb
6694f3b
3f86bf2
16e017d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -264,15 +264,8 @@ Generic methods and generic self | |||||||
You can also define generic methods — just use a type variable in the | ||||||||
method signature that is different from class type variables. In particular, | ||||||||
``self`` may also be generic, allowing a method to return the most precise | ||||||||
type known at the point of access. | ||||||||
|
||||||||
.. note:: | ||||||||
|
||||||||
This feature is experimental. Checking code with type annotations for self | ||||||||
arguments is still not fully implemented. Mypy may disallow valid code or | ||||||||
allow unsafe code. | ||||||||
|
||||||||
In this way, for example, you can typecheck chaining of setter methods: | ||||||||
type known at the point of access. In this way, for example, you can typecheck | ||||||||
chaining of setter methods: | ||||||||
|
||||||||
.. code-block:: python | ||||||||
|
||||||||
|
@@ -335,6 +328,43 @@ possibly by making use of the ``Any`` type. | |||||||
|
||||||||
For some advanced uses of self-types see :ref:`additional examples <advanced_self>`. | ||||||||
|
||||||||
Automatic self types using typing.Self | ||||||||
************************************** | ||||||||
|
||||||||
The patterns described above are quite common, so there is a syntactic sugar | ||||||||
for them introduced in :pep:`673`. Instead of defining a type variable and | ||||||||
using an explicit ``self`` annotation, you can import a magic type ``typing.Self`` | ||||||||
that is automatically transformed into a type variable with an upper bound of | ||||||||
current class, and you don't need an annotation for ``self`` (or ``cls`` for | ||||||||
class methods). The above example can thus be rewritten as: | ||||||||
|
||||||||
.. code-block:: python | ||||||||
|
||||||||
from typing import Self | ||||||||
|
||||||||
class Friend: | ||||||||
other: Self | None = None | ||||||||
|
||||||||
@classmethod | ||||||||
def make_pair(cls) -> tuple[Self, Self]: | ||||||||
a, b = cls(), cls() | ||||||||
a.other = b | ||||||||
b.other = a | ||||||||
return a, b | ||||||||
|
||||||||
class SuperFriend(Friend): | ||||||||
pass | ||||||||
|
||||||||
a, b = SuperFriend.make_pair() | ||||||||
|
||||||||
This is more compact than using explicit type variables, plus additionally | ||||||||
you can use ``Self`` in attribute annotations, not just in methods. | ||||||||
|
||||||||
.. note:: | ||||||||
|
||||||||
This feature is available on Python 3.11 or newer. On older Python versions | ||||||||
you can import backported ``Self`` from latest ``typing_extensions``. | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd suggest replacing 'latest' with 'recent enough' since otherwise it go stale quickly. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, it's misleading to start with "This feature is available on Python 3.11 or newer." Users will think they can't use
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I will make it more clear (or maybe just commit what Jelle proposed) |
||||||||
|
||||||||
.. _variance-of-generics: | ||||||||
|
||||||||
Variance of generic types | ||||||||
|
@@ -548,7 +578,7 @@ Note that class decorators are handled differently than function decorators in | |||||||
mypy: decorating a class does not erase its type, even if the decorator has | ||||||||
incomplete type annotations. | ||||||||
|
||||||||
Suppose we have the following decorator, not type annotated yet, | ||||||||
Suppose we have the following decorator, not type annotated yet, | ||||||||
that preserves the original function's signature and merely prints the decorated function's name: | ||||||||
|
||||||||
.. code-block:: python | ||||||||
|
Uh oh!
There was an error while loading. Please reload this page.