@@ -527,7 +527,7 @@ inferred from a protocol definition. Examples::
527
527
new_sender = sender # OK, type checker finds that 'Sender' is contravariant.
528
528
529
529
class Proto(Protocol[T]):
530
- attr: T
530
+ attr: T # this class is invariant, since it has a mutable attribute
531
531
532
532
var: Proto[float]
533
533
another_var: Proto[int]
@@ -789,20 +789,19 @@ Changes in the typing module
789
789
790
790
The following classes in ``typing`` module will be protocols:
791
791
792
- * ``Hashable ``
793
- * ``SupportsAbs`` (and other ``Supports*`` classes)
792
+ * ``Callable ``
793
+ * ``Awaitable``
794
794
* ``Iterable``, ``Iterator``
795
+ * ``AsyncIterable``, ``AsyncIterator``
796
+ * ``Hashable``
795
797
* ``Sized``
796
798
* ``Container``
797
799
* ``Collection``
798
800
* ``Reversible``
799
801
* ``Sequence``, ``MutableSequence``
800
- * ``AbstractSet``, ``MutableSet``
801
802
* ``Mapping``, ``MutableMapping``
802
- * ``AsyncIterable``, ``AsyncIterator``
803
- * ``Awaitable``
804
- * ``Callable``
805
803
* ``ContextManager``, ``AsyncContextManager``
804
+ * ``SupportsAbs`` (and other ``Supports*`` classes)
806
805
807
806
Most of these classes are small and conceptually simple. It is easy to see
808
807
what are the methods these protocols implement, and immediately recognize
@@ -1119,6 +1118,39 @@ This was rejected for the following reasons:
1119
1118
it has an unsafe override.
1120
1119
1121
1120
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
+
1122
1154
Backwards Compatibility
1123
1155
=======================
1124
1156
@@ -1145,6 +1177,9 @@ https://github.com/ilevkivskyi/typeshed/tree/protocols. Installation steps::
1145
1177
git fetch proto && git checkout proto/protocols
1146
1178
cd .. && git add typeshed && sudo python3 -m pip install -U .
1147
1179
1180
+ The runtime implementation of protocols in ``typing`` module is
1181
+ found at https://github.com/ilevkivskyi/typehinting/tree/protocols.
1182
+
1148
1183
1149
1184
References
1150
1185
==========
0 commit comments