@@ -18,8 +18,8 @@ Abstract
1818
1919This PEP introduces a simple and intuitive way to annotate methods that return
2020an instance of their class. This behaves the same as the ``TypeVar ``-based
21- approach specified in PEP 484 [ # pep-484-self-type ]_ but is more concise and
22- easier to follow.
21+ approach specified in ` PEP 484 < https://www.python.org/dev/peps/ pep-0484 >`_
22+ but is more concise and easier to follow.
2323
2424Motivation
2525==========
@@ -132,13 +132,15 @@ As in the above example, the type checker will correctly infer the type of
132132Usage statistics
133133----------------
134134
135- We analyzed popular open-source projects [#self-type-usage-stats ]_ and found
136- that patterns like the above were used about **40% ** as often as popular types
137- like ``dict `` or ``Callable ``. For example, in typeshed alone, such “Self”
138- types are used 523 times, compared to 1286 uses of ``dict `` and 1314 uses of
139- ``Callable `` as of October 2021 [#callable-dict-usage-stats ]_. This suggests
140- that a ``Self `` type will be used quite often and users will benefit a lot
141- from the simpler approach above.
135+ We `analyzed
136+ <https://github.com/pradeep90/annotation_collector/#self-type-stats> `_ popular
137+ open-source projects and found that patterns like the above were used about
138+ **40% ** as often as popular types like ``dict `` or ``Callable ``. For example,
139+ in typeshed alone, such “Self” types are used 523 times, compared to 1286 uses
140+ of ``dict `` and 1314 uses of ``Callable `` `as of October 2021
141+ <https://github.com/pradeep90/annotation_collector/#overall-stats-in-typeshed> `_.
142+ This suggests that a ``Self `` type will be used quite often and users will
143+ benefit a lot from the simpler approach above.
142144
143145Specification
144146=============
@@ -495,8 +497,9 @@ is treated equivalently to:
495497 return self
496498
497499
498- See [#protocol-self-type ]_ for details on the behavior of TypeVars bound
499- to protocols.
500+ See `PEP 544
501+ <https://www.python.org/dev/peps/pep-0544/#self-types-in-protocols> `_ for
502+ details on the behavior of TypeVars bound to protocols.
500503
501504Checking a class for compatibility with a protocol: If a protocol uses
502505``Self `` in methods or attribute annotations, then a class ``Foo `` is
@@ -753,85 +756,32 @@ Resources
753756=========
754757
755758Similar discussions on a ``Self `` type in Python started in Mypy around 2016:
756- [#mypy1212 ]_. However, the approach ultimately taken there was the bounded
757- ``TypeVar `` approach shown in our "before" examples. Other issues that discuss
758- this include [#mypy2354 ]_.
759+ `Mypy issue #1212 <https://github.com/python/mypy/issues/1212 >`_ - SelfType or
760+ another way to spell "type of self". However, the approach ultimately taken
761+ there was the bounded ``TypeVar `` approach shown in our "before" examples.
762+ Other issues that discuss this include `Mypy issue #2354
763+ <https://github.com/python/mypy/issues/2354> `_ - Self types in generic
764+ classes.
759765
760766**Pradeep ** made a concrete proposal at the PyCon Typing Summit 2021:
761- [#type-variables-for-all ]_ (slides [#type-variables-for-all-slides ]_).
767+ `recorded talk <https://youtu.be/ld9rwCvGdhc?t=3260 >`_, `slides
768+ <https://drive.google.com/file/d/1x-qoDVY_OvLpIV1EwT7m3vm4HrgubHPG/view> `_.
762769
763770**James ** brought up the proposal independently on typing-sig:
764- [ #james- typing-sig] _.
771+ ` Typing-sig thread < https://mail.python.org/archives/list/ typing-sig@python.org/thread/SJAANGA2CWZ6D6TJ7KOPG7PZQC56K73S/#B2CBLQDHXQ5HMFUMS4VNY2D4YDCFT64Q >` _.
765772
766773Other languages have similar ways to express the type of the enclosing class:
767774
768- + TypeScript has the ``this `` type [#typescript-this-type ]_
769- + Rust has the ``Self `` type [#rust-self-type ]_
775+ + TypeScript has the ``this `` type (`TypeScript docs
776+ <https://typescriptlang.org/docs/handbook/2/classes.html#this-types> `_)
777+ + Rust has the ``Self `` type (`Rust docs
778+ <https://doc.rust-lang.org/std/keyword.SelfTy.html> `_)
770779
771780Thanks to the following people for their feedback on the PEP:
772781
773782Jia Chen, Rebecca Chen, Sergei Lebedev, Kaylynn Morgan, Tuomas Suutari, Alex
774783Waygood, Shannon Zhu, and Никита Соболев
775784
776- References
777- ==========
778-
779- .. [#pep-484-self-type ] PEP 484's approach to annotating ``self `` and ``cls ``.
780-
781- https://www.python.org/dev/peps/pep-0484/#annotating-instance-and-class-methods
782-
783- .. [#mypy1212 ] SelfType or another way to spell "type of self"
784-
785- https://github.com/python/mypy/issues/1212
786-
787- .. [#mypy2354 ] Self types in generic classes
788-
789- https://github.com/python/mypy/issues/2354
790-
791- .. [#type-variables-for-all ] Type Variables for All talk
792-
793- https://youtu.be/ld9rwCvGdhc?t=3260
794-
795- .. [#type-variables-for-all-slides ] Slides
796-
797- https://drive.google.com/file/d/1x-qoDVY_OvLpIV1EwT7m3vm4HrgubHPG/view
798-
799- .. [#james-typing-sig ] Thread
800-
801- https://mail.python.org/archives/list/typing-sig@python.org/thread/SJAANGA2CWZ6D6TJ7KOPG7PZQC56K73S/#B2CBLQDHXQ5HMFUMS4VNY2D4YDCFT64Q
802-
803- .. [#property-workaround ] Property workaround
804-
805- https://mypy-play.net/?mypy=latest&python=3.8&gist=ae886111451833e38737721a4153fd96
806-
807- .. [#self-type-usage-stats ] Self type usage stats
808-
809- https://github.com/pradeep90/annotation_collector/#self-type-stats
810-
811- .. [#callable-dict-usage-stats ] ``Callable `` and ``dict `` usage stats
812-
813- https://github.com/pradeep90/annotation_collector/#overall-stats-in-typeshed
814-
815- .. [#protocol-self-type ] Protocol-bound ``TypeVar ``
816-
817- https://www.python.org/dev/peps/pep-0544/#self-types-in-protocols
818-
819- .. [#rust-self-type ] Rust ``Self `` type
820-
821- https://doc.rust-lang.org/std/keyword.SelfTy.html
822-
823- .. [#typescript-this-type ] TypeScript ``this `` type
824-
825- https://typescriptlang.org/docs/handbook/2/classes.html#this-types
826-
827- .. [#mypy-implementation ] Mypy implementation
828-
829- https://github.com/Gobot1234/mypy
830-
831- .. [#self-implementation ] ``Self `` implementation
832-
833- https://github.com/Gobot1234/typing/commit/8b41f2478144fcc32bac7a65afc3e490c24bb068
834-
835785Copyright
836786=========
837787
0 commit comments