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
8000 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 some examples
  • Loading branch information
ilevkivskyi committed Mar 5, 2017
commit 993f7b3d716ab569a778881813353efb8b371980
85 changes: 78 additions & 7 deletions pep-05xx.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ Python-Version: 3.7
Abstract
========

This PEP describes the static and runtime behavior of protocol classes.
PEP 484 has introduced type hints that can be used to specify type metadata
for static type checkers and other third party tools. However, PEP 484
only supports the *nominal* subtyping. In this PEP we specify the static
and runtime semantics of protocol classes that will provide a support for
*structural* subtyping (static duck typing).


Rationale and Goals
Expand Down Expand Up @@ -48,24 +52,87 @@ still be implicitly considered a subtype of both ``Sized`` and
def __iter__(self) -> Iterator[int]: ...


Nominal vs structural subtyping
-------------------------------

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
by the ``typing`` module. Instead, it is proposed that the protocol classes
complement normal classes, and the choice is left for a user to decide where
to apply a particular solution. See section "Rejected ideas" for additional
motivation.


Non-Goals
---------

Runtime protocol classes are simple ABCs. Moreover as PEP 484 and PEP 526
stated they are **completely optional**.


Nominal vs structural subtyping
===============================


Existing approaches to structural subtyping
===========================================

* ``zope.interace``
* TypeScript
* ``zope.interace``::

from zope.interface import Interface
from zope.interface import Attribute
from zope.interface import implements

class IHost(Interface):
"""A host object"""

name = Attribute("Name of host")

def goodmorning(guest):
"""Say good morning to guest"""

class Host(object):

implements(IHost)

name = u''

def goodmorning(self, guest):
return "Good morning, %s!" % guest

plus much more detailed runtime constraints::

from zope.interface import invariant

def contacts_invariant(obj):
if not (obj.email or obj.phone):
raise Exception("At least one contact info is required")

class IPerson(Interface):

name = Attribute("Name")
email = Attribute("Email Address")
phone = Attribute("Phone Number")

invariant(contacts_invariant)

However, this all requires runtime validation.

* TypeScript::

interface LabelledValue {
label: string;
}

function printLabel(labelledObj: LabelledValue) {
console.log(labelledObj.label);
}

let myObj = {size: 10, label: "Size 10 Object"};
printLabel(myObj);

* Go - everything is an interface
* ABCs

require explicit subclassing or registration.

* ``collections.abc`` (with ``__subclasshook__``)


Expand Down Expand Up @@ -270,6 +337,8 @@ provide a default implementation that could be used in explicit subclasses.

Overall, I'm probably the least happy about this part of the proposal.

"Private" variables on self? treat (double?) underscores as private?


Explicitly declaring implementation
-----------------------------------
Expand Down Expand Up @@ -411,6 +480,8 @@ We need this fallback option anyway for backward compatibility.

``Type[...]`` accepts only non-abstract (non-protocol?) classes

copy all stuff from my PR


Runtime behavior of protocol classes
====================================
Expand Down
0