Skip to content

Few more updates to PEP 544: Protocols #246

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 27, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 42 additions & 7 deletions pep-0544.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
=======================

Expand All @@ -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
==========
Expand Down