diff --git a/pep-0544.txt b/pep-0544.txt index 480c1cf71..6b1b51fb4 100644 --- a/pep-0544.txt +++ b/pep-0544.txt @@ -527,7 +527,7 @@ inferred from a protocol definition. Examples:: new_sender = sender # OK, type checker finds that 'Sender' is contravariant. class Proto(Protocol[T]): - attr: T + attr: T # this class is invariant, since it has a mutable attribute var: Proto[float] another_var: Proto[int] @@ -789,20 +789,19 @@ Changes in the typing module The following classes in ``typing`` module will be protocols: -* ``Hashable`` -* ``SupportsAbs`` (and other ``Supports*`` classes) +* ``Callable`` +* ``Awaitable`` * ``Iterable``, ``Iterator`` +* ``AsyncIterable``, ``AsyncIterator`` +* ``Hashable`` * ``Sized`` * ``Container`` * ``Collection`` * ``Reversible`` * ``Sequence``, ``MutableSequence`` -* ``AbstractSet``, ``MutableSet`` * ``Mapping``, ``MutableMapping`` -* ``AsyncIterable``, ``AsyncIterator`` -* ``Awaitable`` -* ``Callable`` * ``ContextManager``, ``AsyncContextManager`` +* ``SupportsAbs`` (and other ``Supports*`` classes) Most of these classes are small and conceptually simple. It is easy to see what are the methods these protocols implement, and immediately recognize @@ -1119,6 +1118,39 @@ This was rejected for the following reasons: it has an unsafe override. +Support adapters and adaptation +------------------------------- + +Adaptation was proposed by PEP 246 (rejected) and is supported by +``zope.interface``, see https://docs.zope.org/zope.interface/adapter.html. +Adapters is quite an advanced concept, and PEP 484 supports unions and +generic aliases that can be used instead of adapters. This can be illustrated +with an example of ``Iterable`` protocol, there is another way of supporting +iteration by providing ``__getitem__`` and ``__len__``. If a function +supports both this way and the now standard ``__iter__`` method, then it could +be annotated by a union type:: + + class OldIterable(Sized, Protocol[T]): + def __getitem__(self, item: int) -> T: ... + + CompatIterable = Union[Iterable[T], OldIterable[T]] + + class A: + def __iter__(self) -> Iterator[str]: ... + class B: + def __len__(self) -> int: ... + def __getitem__(self, item: int) -> str: ... + + def iterate(it: CompatIterable[str]) -> None: + ... + + iterate(A()) # OK + iterate(B()) # OK + +Since there is a reasonable alternative for such cases with existing tooling, +it is therefore proposed not to include adaptation in this PEP. + + Backwards Compatibility ======================= @@ -1145,6 +1177,9 @@ https://github.com/ilevkivskyi/typeshed/tree/protocols. Installation steps:: git fetch proto && git checkout proto/protocols cd .. && git add typeshed && sudo python3 -m pip install -U . +The runtime implementation of protocols in ``typing`` module is +found at https://github.com/ilevkivskyi/typehinting/tree/protocols. + References ==========