8000 PEP 544: Protocols by ilevkivskyi · Pull Request #224 · python/peps · GitHub
[go: up one dir, main page]

Skip to content

PEP 544: Protocols #224

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 40 commits into from
Mar 18, 2017
Merged
Changes from 1 commit
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
3fa3e48
Collect various ideas
ilevkivskyi Mar 5, 2017
51b0ca0
Some formatting and reordering
ilevkivskyi Mar 5, 2017
993f7b3
Add some examples
ilevkivskyi Mar 5, 2017
e384336
Add planned link templates
ilevkivskyi Mar 6, 2017
7ea5d41
Add links + minor changes
Mar 6, 2017
cdcf62f
Polishing rationale
Mar 6, 2017
1ffed9b
Some more reshuffling and formatting
Mar 6, 2017
72ceae6
Add more examples
Mar 6, 2017
6bea2e8
Add more examples to existing approaches
Mar 6, 2017
d5972c3
Typos, reordering, and few more details (backport)
ilevkivskyi Mar 6, 2017
57d375f
Update list of protocols in typing
ilevkivskyi Mar 6, 2017
9d4d685
Defining protocols plus minor changes and formatting
ilevkivskyi Mar 7, 2017
82258d5
Explicitly declaring implementation and other changes
ilevkivskyi Mar 7, 2017
5d9fb7c
More polishing
ilevkivskyi Mar 7, 2017
a6e6d9e
Edit rejected/postponed ideas
ilevkivskyi Mar 7, 2017
3175013
Runtime things, reorder links
ilevkivskyi Mar 7, 2017
cbff669
Runtime decorator
ilevkivskyi Mar 7, 2017
dfccd06
Backward compatible part and last bits
Mar 8, 2017
60f4d52
Some clarifications
ilevkivskyi Mar 9, 2017
60e7f7f
Add links in text
ilevkivskyi Mar 9, 2017
c90aa1c
Caption style, add cross-refs
Mar 9, 2017
b008de1
Remove redundant links; + minor changes
ilevkivskyi Mar 10, 2017
02cca5c
One more tiny change
ilevkivskyi Mar 10, 2017
7d89b6b
Merge remote-tracking branch 'upstream/master' into protocols
ilevkivskyi Mar 10, 2017
0f3732a
Copyediting changes
JelleZijlstra Mar 10, 2017
95fbf58
Merge pull request #1 from JelleZijlstra/patch-2
ilevkivskyi Mar 10, 2017
cb65bff
Rename PEP with a valid number to get the build running
ilevkivskyi Mar 10, 2017
817bf2f
Reflow to 79 characters
ilevkivskyi Mar 10, 2017
2d89ba9
fix typo
JelleZijlstra Mar 10, 2017
0efcbff
Some grammar tweaks
brettcannon Mar 10, 2017
ebd4b17
Merge pull request #3 from brettcannon/patch-1
ilevkivskyi Mar 10, 2017
0de36be
Implement Guido's idea of EIBTI plus minor comments
ilevkivskyi Mar 11, 2017
767c58b
Fix typo
ilevkivskyi Mar 11, 2017
efc3154
Make implementation enforcement optional; fix order of Protocolbase
ilevkivskyi Mar 12, 2017
7d714c3
Add missing @abstractmethod decorators
ilevkivskyi Mar 13, 2017
d4ab050
Minor clarification
ilevkivskyi Mar 13, 2017
d9d21c2
Implement Jukka's and David's comments; few more minor things
ilevkivskyi Mar 16, 2017
4dfbfb2
Implement most comments by Łukasz; few more to do
ilevkivskyi Mar 17, 2017
d51420e
More changes in response to comments
Mar 18, 2017
f6240c8
Remove one reamining 'All'
ilevkivskyi Mar 18, 2017
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
Prev Previous commit
Next Next commit
Add links + minor changes
  • Loading branch information
Ivan Levkivskyi committed Mar 6, 2017
commit 7ea5d41b74fb89f7443fa7312ccd534313bf55b4
73 changes: 40 additions & 33 deletions pep-05xx.txt
< 8000 td id="diff-d5b7b94bb4f19e9fd966ddf3468f761e24b7a3cbd190ad0bdcb9deb5efe43c73R623" data-line-number="623" class="blob-num blob-num-context js-linkable-line-number js-blob-rnum">
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ still be implicitly considered a subtype of both ``Sized`` and
Nominal vs structural subtyping
-------------------------------

Although structural subtyping is natural for Python programmers
(explain once more what it is)...
As discussed in PEP 483, both nominal and structural subtyping have their
strengths and weaknesses. Therefore, in this PEP do we not propose to propose
to replace the nominal subtyping described by PEP 484 and currently supported
Expand All @@ -74,7 +76,7 @@ stated they are **completely optional**.
Existing approaches to structural subtyping
===========================================

* ``zope.interace``::
* Zope interfaces::

from zope.interface import Interface
from zope.interface import Attribute
Expand All @@ -97,7 +99,7 @@ Existing approaches to structural subtyping
def goodmorning(self, guest):
return "Good morning, %s!" % guest

plus much more detailed runtime constraints::
plus interface contracts/constraints::

from zope.interface import invariant

Expand All @@ -113,7 +115,7 @@ plus much more detailed runtime constraints::

invariant(contacts_invariant)

However, this all requires runtime validation.
Even more is possible. However, this all requires runtime validation.

* TypeScript::

Expand All @@ -128,12 +130,14 @@ However, this all requires runtime validation.
let myObj = {size: 10, label: "Size 10 Object"};
printLabel(myObj);

In addition optional members, always errors on reduntant members.

* Go - everything is an interface
* ABCs
* Python abstract base classes

require explicit subclassing or registration.

* ``collections.abc`` (with ``__subclasshook__``)
* Abstract classes in ``collections.abc`` (with ``__subclasshook__``)


Specification
Expand All @@ -142,28 +146,30 @@ Specification
Terminology
-----------

Let's call them protocols. The reason is that the term iterator protocol,
We propose to use the term *protocols* types supoorting structural subtyping.
The reason is that the term iterator protocol,
for example, is widely understood in the community, and coming up with
a new term for this concept in a statically typed context would just create
confusion.

This has the drawback that the term 'protocol' becomes overloaded with
This has the drawback that the term *protocol* becomes overloaded with
two subtly different meanings: the first is the traditional, well-known but
slightly fuzzy concept of protocols such as iterable; the second is the more
explicitly defined concept of protocols in statically typed code
(or more generally in code that just uses the typing module).
I argue that the distinction isn't important most of the time, and in other
cases people can just add a qualifier such as "protocol classes"
(for the new-style protocols) or "traditional/non-class/implicit protocols".

Some terminology could be useful here for clarity. If a class includes a
protocol in its MRO, the class is an (explicit) subclass of the protocol.
If a class ia a structural subtype of a protocol, it is said to implement
the protocol and to be compatible with a protocol.
slightly fuzzy concept of protocols such as iterator; the second is the more
explicitly defined concept of protocols in statically typed code.
The distinction is not important most of the time, and in other
cases we propose to just add a qualifier such as *protocol classes*
when refering to the static type concept.

If a class includes a protocol in its MRO, the class is an called an *explicit*
subclass of the protocol. If a class ia a structural subtype of a protocol,
it is said to implement the protocol and to be compatible with a protocol.
If a class is compatible with a protocol but the protocol is not included
in the MRO, the class is an implicit subclass of the protocol.
in the MRO, the class is an *implicit* subclass of the protocol.

Protocol members.
The attributes (variables and methods) of a protocol that are mandatory
for other class in order to be considered a subtype are called
protocol members. Other members defined in a protocol class are called
non-protocol members.


Defining a Protocol
Expand Down Expand Up @@ -617,7 +623,8 @@ do not use them yet.
Use assignments to check explicitly that a class implements a protocol
----------------------------------------------------------------------

I've seen a similar pattern in some Go code that I've reviewed. Example::
I've seen a similar pattern in some Go code that I've reviewed [golang].
Example::

class A:
def __len__(self) -> float:
Expand All @@ -642,8 +649,8 @@ References
.. [mypy]
http://mypy-lang.org

.. [wiki-structural-subtypin]
http://en.wikipedia.org/wiki/...
.. [wiki-structural-subtyping]
https://en.wikipedia.org/wiki/Structural_type_system

.. [typeshed]
https://github.com/python/typeshed/
Expand All @@ -652,31 +659,31 @@ References
https://github.com/python/typing/issues

.. [pep482]
https://hg.python.org/peps/file/tip/pep-0482.txt
https://www.python.org/dev/peps/pep-0482/

.. [pep483]
https://hg.python.org/peps/file/tip/pep-0483.txt
https://www.python.org/dev/peps/pep-0483/

.. [pep484]
https://hg.python.org/peps/file/tip/pep-0484.txt
https://www.python.org/dev/peps/pep-0484/

.. [pep526]
https://hg.python.org/peps/file/tip/pep-0526.txt
https://www.python.org/dev/peps/pep-0526/

.. [typescript]
https://...
https://www.typescriptlang.org/docs/handbook/interfaces.html

.. [golang]
https://...
https://golang.org/doc/effective_go.html#interfaces_and_types

.. [zope-interface]
https://...
.. [zope-interfaces]
https://zopeinterface.readthedocs.io/en/latest/

.. [abstract-base-classes]
https://...
https://docs.python.org/3/library/abc.html

.. [collections-abc]
https://...
https://docs.python.org/3/library/collections.abc.html


Copyright
Expand Down
0