mirror of
https://github.com/python/cpython.git
synced 2025-12-04 00:30:19 +00:00
bpo-46534: Implement PEP 673 Self in typing.py (GH-30924)
Co-authored-by: Pradeep Kumar Srinivasan <gohanpra@gmail.com> Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
This commit is contained in:
parent
39dec1c09c
commit
7ba1cc8049
5 changed files with 116 additions and 2 deletions
|
|
@ -68,6 +68,8 @@ annotations. These include:
|
|||
*Introducing* :data:`TypeAlias`
|
||||
* :pep:`647`: User-Defined Type Guards
|
||||
*Introducing* :data:`TypeGuard`
|
||||
* :pep:`673`: Self type
|
||||
*Introducing* :data:`Self`
|
||||
|
||||
.. _type-aliases:
|
||||
|
||||
|
|
@ -585,6 +587,51 @@ These can be used as types in annotations and do not support ``[]``.
|
|||
.. versionadded:: 3.5.4
|
||||
.. versionadded:: 3.6.2
|
||||
|
||||
.. data:: Self
|
||||
|
||||
Special type to represent the current enclosed class.
|
||||
For example::
|
||||
|
||||
from typing import Self
|
||||
|
||||
class Foo:
|
||||
def returns_self(self) -> Self:
|
||||
...
|
||||
return self
|
||||
|
||||
|
||||
This annotation is semantically equivalent to the following,
|
||||
albeit in a more succinct fashion::
|
||||
|
||||
from typing import TypeVar
|
||||
|
||||
Self = TypeVar("Self", bound="Foo")
|
||||
|
||||
class Foo:
|
||||
def returns_self(self: Self) -> Self:
|
||||
...
|
||||
return self
|
||||
|
||||
In general if something currently follows the pattern of::
|
||||
|
||||
class Foo:
|
||||
def return_self(self) -> "Foo":
|
||||
...
|
||||
return self
|
||||
|
||||
You should use use :data:`Self` as calls to ``SubclassOfFoo.returns_self`` would have
|
||||
``Foo`` as the return type and not ``SubclassOfFoo``.
|
||||
|
||||
Other common use cases include:
|
||||
|
||||
- :class:`classmethod`\s that are used as alternative constructors and return instances
|
||||
of the ``cls`` parameter.
|
||||
- Annotating an :meth:`object.__enter__` method which returns self.
|
||||
|
||||
For more information, see :pep:`673`.
|
||||
|
||||
.. versionadded:: 3.11
|
||||
|
||||
.. data:: TypeAlias
|
||||
|
||||
Special annotation for explicitly declaring a :ref:`type alias <type-aliases>`.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue