Skip to content

Commit ea2cd15

Browse files
ilevkivskyibrettcannon
authored andcommitted
Few more updates to PEP 544: Protocols (#246)
* Drop Set's from protocols; add adapters to rejected ideas * Add link to runtime implementation
1 parent a7e294d commit ea2cd15

File tree

1 file changed

+42
-7
lines changed

1 file changed

+42
-7
lines changed

pep-0544.txt

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ inferred from a protocol definition. Examples::
527527
new_sender = sender # OK, type checker finds that 'Sender' is contravariant.
528528

529529
class Proto(Protocol[T]):
530-
attr: T
530+
attr: T # this class is invariant, since it has a mutable attribute
531531

532532
var: Proto[float]
533533
another_var: Proto[int]
@@ -789,20 +789,19 @@ Changes in the typing module
789789

790790
The following classes in ``typing`` module will be protocols:
791791

792-
* ``Hashable``
793-
* ``SupportsAbs`` (and other ``Supports*`` classes)
792+
* ``Callable``
793+
* ``Awaitable``
794794
* ``Iterable``, ``Iterator``
795+
* ``AsyncIterable``, ``AsyncIterator``
796+
* ``Hashable``
795797
* ``Sized``
796798
* ``Container``
797799
* ``Collection``
798800
* ``Reversible``
799801
* ``Sequence``, ``MutableSequence``
800-
* ``AbstractSet``, ``MutableSet``
801802
* ``Mapping``, ``MutableMapping``
802-
* ``AsyncIterable``, ``AsyncIterator``
803-
* ``Awaitable``
804-
* ``Callable``
805803
* ``ContextManager``, ``AsyncContextManager``
804+
* ``SupportsAbs`` (and other ``Supports*`` classes)
806805

807806
Most of these classes are small and conceptually simple. It is easy to see
808807
what are the methods these protocols implement, and immediately recognize
@@ -1119,6 +1118,39 @@ This was rejected for the following reasons:
11191118
it has an unsafe override.
11201119

11211120

1121+
Support adapters and adaptation
1122+
-------------------------------
1123+
1124+
Adaptation was proposed by PEP 246 (rejected) and is supported by
1125+
``zope.interface``, see https://docs.zope.org/zope.interface/adapter.html.
1126+
Adapters is quite an advanced concept, and PEP 484 supports unions and
1127+
generic aliases that can be used instead of adapters. This can be illustrated
1128+
with an example of ``Iterable`` protocol, there is another way of supporting
1129+
iteration by providing ``__getitem__`` and ``__len__``. If a function
1130+
supports both this way and the now standard ``__iter__`` method, then it could
1131+
be annotated by a union type::
1132+
1133+
class OldIterable(Sized, Protocol[T]):
1134+
def __getitem__(self, item: int) -> T: ...
1135+
1136+
CompatIterable = Union[Iterable[T], OldIterable[T]]
1137+
1138+
class A:
1139+
def __iter__(self) -> Iterator[str]: ...
1140+
class B:
1141+
def __len__(self) -> int: ...
1142+
def __getitem__(self, item: int) -> str: ...
1143+
1144+
def iterate(it: CompatIterable[str]) -> None:
1145+
...
1146+
1147+
iterate(A()) # OK
1148+
iterate(B()) # OK
1149+
1150+
Since there is a reasonable alternative for such cases with existing tooling,
1151+
it is therefore proposed not to include adaptation in this PEP.
1152+
1153+
11221154
Backwards Compatibility
11231155
=======================
11241156

@@ -1145,6 +1177,9 @@ https://github.com/ilevkivskyi/typeshed/tree/protocols. Installation steps::
11451177
git fetch proto && git checkout proto/protocols
11461178
cd .. && git add typeshed && sudo python3 -m pip install -U .
11471179

1180+
The runtime implementation of protocols in ``typing`` module is
1181+
found at https://github.com/ilevkivskyi/typehinting/tree/protocols.
1182+
11481183

11491184
References
11501185
==========

0 commit comments

Comments
 (0)