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:
parent
9c06059758
commit
da41a811d7
27
pep-0544.txt
27
pep-0544.txt
|
@ -586,6 +586,30 @@ Continuing the previous example::
|
||||||
walk(tree) # OK, 'Tree[float]' is a subtype of 'Traversable'
|
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
|
Using Protocols
|
||||||
===============
|
===============
|
||||||
|
|
||||||
|
@ -1369,6 +1393,9 @@ References
|
||||||
.. [elsewhere]
|
.. [elsewhere]
|
||||||
https://github.com/python/peps/pull/224
|
https://github.com/python/peps/pull/224
|
||||||
|
|
||||||
|
.. [self-types]
|
||||||
|
https://www.python.org/dev/peps/pep-0484/#annotating-instance-and-class-methods
|
||||||
|
|
||||||
|
|
||||||
Copyright
|
Copyright
|
||||||
=========
|
=========
|
||||||
|
|
Loading…
Reference in New Issue