@@ -24,7 +24,7 @@ static and runtime semantics of protocol classes that will provide a support
24
24
for *structural* subtyping (static duck typing).
25
25
26
26
27
- .. _rationale :
27
+ .. _PEP 544 rationale :
28
28
29
29
Rationale and Goals
30
30
===================
@@ -86,8 +86,8 @@ subtyping have their strengths and weaknesses. Therefore, in this PEP we
86
86
*do not propose* to replace the nominal subtyping described by :pep:`484` with
87
87
structural subtyping completely. Instead, protocol classes as specified in
88
88
this PEP complement normal classes, and users are free to choose
89
- where to apply a particular solution. See section on `rejected`_ ideas at the
90
- end of this PEP for additional motivation.
89
+ where to apply a particular solution. See section on `rejected
90
+ <PEP 544 rejected_>`_ ideas at the end of this PEP for additional motivation.
91
91
92
92
93
93
Non-goals
@@ -181,8 +181,9 @@ approaches related to structural subtyping in Python and other languages:
181
181
assert issubclass(tuple, MyTuple)
182
182
assert isinstance((), MyTuple)
183
183
184
- As mentioned in the `rationale`_, we want to avoid such necessity, especially
185
- in static context. However, in a runtime context, ABCs are good candidates for
184
+ As mentioned in the `rationale <PEP 544 rationale_>`_,
185
+ we want to avoid such necessity, especially in static context.
186
+ However, in a runtime context, ABCs are good candidates for
186
187
protocol classes and they are already used extensively in
187
188
the ``typing`` module.
188
189
@@ -200,8 +201,9 @@ approaches related to structural subtyping in Python and other languages:
200
201
assert isinstance(MyIterable(), Iterable)
201
202
202
203
Such behavior seems to be a perfect fit for both runtime and static behavior
203
- of protocols. As discussed in `rationale`_, we propose to add static support
204
- for such behavior. In addition, to allow users to achieve such runtime
204
+ of protocols. As discussed in `rationale <PEP 544 rationale_>`_,
205
+ we propose to add static support for such behavior.
206
+ In addition, to allow users to achieve such runtime
205
207
behavior for *user-defined* protocols a special ``@runtime_checkable`` decorator
206
208
will be provided, see detailed `discussion`_ below.
207
209
@@ -225,9 +227,9 @@ approaches related to structural subtyping in Python and other languages:
225
227
prohibits redundant members in implementations. While the idea of
226
228
optional members looks interesting, it would complicate this proposal and
227
229
it is not clear how useful it will be. Therefore, it is proposed to postpone
228
- this; see `rejected`_ ideas. In general, the idea of static protocol
229
- checking without runtime implications looks reasonable, and basically
230
- this proposal follows the same line.
230
+ this; see `rejected <PEP 544 rejected_> `_ ideas. In general, the idea of
231
+ static protocol checking without runtime implications looks reasonable,
232
+ and basically this proposal follows the same line.
231
233
232
234
* Go [golang]_ uses a more radical approach and makes interfaces the primary
233
235
way to provide type information. Also, assignments are used to explicitly
@@ -242,11 +244,9 @@ approaches related to structural subtyping in Python and other languages:
242
244
}
243
245
244
246
Both these ideas are questionable in the context of this proposal. See
245
- the section on `rejected`_ ideas.
247
+ the section on `rejected <PEP 544 rejected_> `_ ideas.
246
248
247
249
248
- .. _specification:
249
-
250
250
Specification
251
251
=============
252
252
@@ -496,8 +496,9 @@ considering subtyping, since structural compatibility is
496
496
the criterion, not the MRO.
497
497
498
498
If ``Protocol`` is included in the base class list, all the other base classes
499
- must be protocols. A protocol can't extend a regular class, see `rejected`_
500
- ideas for rationale. Note that rules around explicit subclassing are different
499
+ must be protocols. A protocol can't extend a regular class, see `rejected
500
+ <PEP 544 rejected_>`_ ideas for rationale.
501
+ Note that rules around explicit subclassing are different
501
502
from regular ABCs, where abstractness is simply defined by having at least one
502
503
abstract method being unimplemented. Protocol classes must be marked
503
504
*explicitly*.
@@ -551,7 +552,7 @@ the declared variance. Examples::
551
552
552
553
Note that unlike nominal classes, de facto covariant protocols cannot be
553
554
declared as invariant, since this can break transitivity of subtyping
554
- (see `rejected`_ ideas for details). For example::
555
+ (see `rejected <PEP 544 rejected_> `_ ideas for details). For example::
555
556
556
557
T = TypeVar('T')
557
558
@@ -732,7 +733,7 @@ Example::
732
733
733
734
If this will prove to be a widely used scenario, then a special
734
735
intersection type construct could be added in future as specified by :pep:`483`,
735
- see `rejected`_ ideas for more details.
736
+ see `rejected <PEP 544 rejected_> `_ ideas for more details.
736
737
737
738
738
739
``Type[]`` and class objects vs protocols
@@ -888,7 +889,8 @@ that provides the same semantics for class and instance checks as for
888
889
assert isinstance(open('some/file'), SupportsClose)
889
890
890
891
Note that instance checks are not 100% reliable statically, this is why
891
- this behavior is opt-in, see section on `rejected`_ ideas for examples.
892
+ this behavior is opt-in, see section on `rejected <PEP 544 rejected_>`_
893
+ ideas for examples.
892
894
The most type checkers can do is to treat ``isinstance(obj, Iterator)``
893
895
roughly as a simpler way to write
894
896
``hasattr(x, '__iter__') and hasattr(x, '__next__')``. To minimize
@@ -1027,7 +1029,7 @@ at runtime in this case). But together with other introspection tools this
1027
1029
give a reasonable perspective for runtime type checking tools.
1028
1030
1029
1031
1030
- .. _rejected :
1032
+ .. _PEP 544 rejected :
1031
1033
1032
1034
Rejected/Postponed Ideas
1033
1035
========================
@@ -1498,14 +1500,3 @@ Copyright
1498
1500
=========
1499
1501
1500
1502
This document has been placed in the public domain.
1501
-
1502
-
1503
-
1504
- ..
1505
- Local Variables:
1506
- mode: indented-text
1507
- indent-tabs-mode: nil
1508
- sentence-end-double-space: t
1509
- fill-column: 70
1510
- coding: utf-8
1511
- End:
0 commit comments