PEP 544: Specify self-types in protocols (#647)

This is already implemented in mypy in exactly the same way it works for nominal classes (and thus shares most bugs of self-types in nominal classes).
This commit is contained in:
Ivan Levkivskyi 2018-05-10 18:01:32 -04:00 committed by Guido van Rossum
parent 9c06059758
commit da41a811d7
1 changed files with 27 additions and 0 deletions

View File

@ -586,6 +586,30 @@ Continuing the previous example::
walk(tree) # OK, 'Tree[float]' is a subtype of 'Traversable'
Self-types in protocols
-----------------------
The self-types in protocols follow the corresponding specification
[self-types]_ of PEP 484. For example::
C = TypeVar('C', bound='Copyable')
class Copyable(Protocol):
def copy(self: C) -> C:
class One:
def copy(self) -> 'One':
...
T = TypeVar('T', bound='Other')
class Other:
def copy(self: T) -> T:
...
c: Copyable
c = One() # OK
c = Other() # Also OK
Using Protocols
===============
@ -1369,6 +1393,9 @@ References
.. [elsewhere]
https://github.com/python/peps/pull/224
.. [self-types]
https://www.python.org/dev/peps/pep-0484/#annotating-instance-and-class-methods
Copyright
=========