From da41a811d7e53a5e01b1d7bdacd2bff806167b4b Mon Sep 17 00:00:00 2001 From: Ivan Levkivskyi Date: Thu, 10 May 2018 18:01:32 -0400 Subject: [PATCH] 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). --- pep-0544.txt | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/pep-0544.txt b/pep-0544.txt index e8023ae11..079a55354 100644 --- a/pep-0544.txt +++ b/pep-0544.txt @@ -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 =========