Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
gvanrossum merged 40 commits intopython:masterfromilevkivskyi:protocols
Mar 18, 2017
Merged
Changes from1 commit
Commits
Show all changes
40 commits
Select commitHold shift + click to select a range
3fa3e48
Collect various ideas
ilevkivskyiMar 5, 2017
51b0ca0
Some formatting and reordering
ilevkivskyiMar 5, 2017
993f7b3
Add some examples
ilevkivskyiMar 5, 2017
e384336
Add planned link templates
ilevkivskyiMar 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)
ilevkivskyiMar 6, 2017
57d375f
Update list of protocols in typing
ilevkivskyiMar 6, 2017
9d4d685
Defining protocols plus minor changes and formatting
ilevkivskyiMar 7, 2017
82258d5
Explicitly declaring implementation and other changes
ilevkivskyiMar 7, 2017
5d9fb7c
More polishing
ilevkivskyiMar 7, 2017
a6e6d9e
Edit rejected/postponed ideas
ilevkivskyiMar 7, 2017
3175013
Runtime things, reorder links
ilevkivskyiMar 7, 2017
cbff669
Runtime decorator
ilevkivskyiMar 7, 2017
dfccd06
Backward compatible part and last bits
Mar 8, 2017
60f4d52
Some clarifications
ilevkivskyiMar 9, 2017
60e7f7f
Add links in text
ilevkivskyiMar 9, 2017
c90aa1c
Caption style, add cross-refs
Mar 9, 2017
b008de1
Remove redundant links; + minor changes
ilevkivskyiMar 10, 2017
02cca5c
One more tiny change
ilevkivskyiMar 10, 2017
7d89b6b
Merge remote-tracking branch 'upstream/master' into protocols
ilevkivskyiMar 10, 2017
0f3732a
Copyediting changes
JelleZijlstraMar 10, 2017
95fbf58
Merge pull request #1 from JelleZijlstra/patch-2
ilevkivskyiMar 10, 2017
cb65bff
Rename PEP with a valid number to get the build running
ilevkivskyiMar 10, 2017
817bf2f
Reflow to 79 characters
ilevkivskyiMar 10, 2017
2d89ba9
fix typo
JelleZijlstraMar 10, 2017
0efcbff
Some grammar tweaks
brettcannonMar 10, 2017
ebd4b17
Merge pull request #3 from brettcannon/patch-1
ilevkivskyiMar 10, 2017
0de36be
Implement Guido's idea of EIBTI plus minor comments
ilevkivskyiMar 11, 2017
767c58b
Fix typo
ilevkivskyiMar 11, 2017
efc3154
Make implementation enforcement optional; fix order of Protocolbase
ilevkivskyiMar 12, 2017
7d714c3
Add missing @abstractmethod decorators
ilevkivskyiMar 13, 2017
d4ab050
Minor clarification
ilevkivskyiMar 13, 2017
d9d21c2
Implement Jukka's and David's comments; few more minor things
ilevkivskyiMar 16, 2017
4dfbfb2
Implement most comments by Łukasz; few more to do
ilevkivskyiMar 17, 2017
d51420e
More changes in response to comments
Mar 18, 2017
f6240c8
Remove one reamining 'All'
ilevkivskyiMar 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
PrevPrevious commit
NextNext commit
Add links in text
  • Loading branch information
@ilevkivskyi
ilevkivskyi committedMar 9, 2017
commit60e7f7fcd9adac85825535ea0ac5289f221fa3de
82 changes: 43 additions & 39 deletionspep-05xx.txt
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -24,11 +24,11 @@ for *structural* subtyping (static duck typing).
Rationale and Goals
===================

Currently, PEP 484 and ``typing`` module define abstract base classes for
several common Python protocols such as ``Iterable`` and ``Sized``.
The problem with them is that a class has to be explicitly marked to support
them, which is arguably unpythonic and unlike what one would normally do in
idiomatic dynamically typed Python code. For example,
Currently, PEP 484[pep484]_and ``typing`` module[typing]_define abstract
base classes forseveral common Python protocols such as ``Iterable`` and
``Sized``.The problem with them is that a class has to be explicitly marked
to supportthem, which is arguably unpythonic and unlike what one would
normally do inidiomatic dynamically typed Python code. For example,
this conforms to the PEP 484::

from typing import Sized, Iterable, Iterator
Expand All@@ -46,7 +46,7 @@ runtime costs. The intention of this PEP is to solve all these problems
by allowing to write the above code without explicit base classes in
the class definition, and ``Bucket`` would still be implicitly considered
a subtype of both ``Sized`` and ``Iterable[int]`` by static type checkers
using structural subtyping::
using structural[wiki-structural-subtyping]_subtyping::

from typing import Iterator

Expand All@@ -71,13 +71,13 @@ Nominal vs structural subtyping
The structural subtyping is natural for Python programmers since it matches
the runtime semantics of duck typing: an object that has certain properties
is treated independently of its actual runtime class.
However, as discussed in PEP 483, both nominal and structural subtyping have
their strengths and weaknesses. Therefore, in this PEPdo we not propose
to replace the nominal subtyping described by PEP 484 with structural
subtyping completely. 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.
However, as discussed in PEP 483 [pep483]_, both nominal and structural
subtyping havetheir strengths and weaknesses. Therefore, in this PEPwe
*do not propose*to replace the nominal subtyping described by PEP 484 with
structuralsubtyping completely. Instead, it is proposed that the protocol
classescomplement normal classes, and the choice is left for a user to decide
whereto apply a particular solution. See section "Rejected ideas" for
additionalmotivation.


Non-Goals
Expand All@@ -88,8 +88,8 @@ sophisticated runtime instance and class checks against protocol classes.
This would be difficult and error-prone and will contradict the logic
of PEP 484. As well, following PEP 484 and PEP 526 we state that protocols are
**completely optional** and there is no intent to make them required.
No runtime semantics will be imposed for variables or parameters annotated with
a protocol class, the actual checks will be performed by third party type
No runtime semantics will be imposed for variables or parameters annotated
witha protocol class, the actual checks will be performed by third party type
checkers and other tools.


Expand All@@ -99,10 +99,10 @@ Existing approaches to structural subtyping
Before describing the actual specification, we review existing approaches
related to structural subtyping in Python and other languages:

* Zope interfaces was one of the first widely used approaches to
structural subtyping in Python. It is implemented by providing special
classes to distinguish interface classes from normal classes, to mark
interface attributes and to explicitly declare implementation.
* Zope interfaces[zope-interfaces]_was one of the first widely used
approaches tostructural subtyping in Python. It is implemented by providing
specialclasses to distinguish interface classes from normal classes,
to markinterface attributes and to explicitly declare implementation.
For example::

from zope.interface import Interface, Attribute, implements
Expand DownExpand Up@@ -146,10 +146,10 @@ related to structural subtyping in Python and other languages:
with a special base class is reasonable and easy to implement both
statically and at runtime.

* Python abstract base classes are the standard library tool to provide some
functionality similar to structural subtyping. The drawback of this
approach is the necessity to either subclass the abstract class or
register an implementation explicitly::
* Python abstract base classes[abstract-base-classes]_are the standard
library tool to provide somefunctionality similar to structural subtyping.
The drawback of thisapproach is the necessity to either subclass
the abstract class orregister an implementation explicitly::

from abc import ABCMeta

Expand All@@ -165,9 +165,10 @@ related to structural subtyping in Python and other languages:
static context. However, in runtime context, ABCs are good candidates for
protocol classes and are already used extensively in ``typing`` module.

* Abstract classes defined in ``collections.abc`` module are slightly more
advanced since they implement a custom ``__subclasshook__()`` method that
allows runtime structural checks without explicit registration::
* Abstract classes defined in ``collections.abc`` module [collections-abc]_
are slightly more advanced since they implement a custom
``__subclasshook__()`` method that allows runtime structural checks without
explicit registration::

from collections.abc import Iterable

Expand All@@ -185,9 +186,9 @@ related to structural subtyping in Python and other languages:
that mimics the one in ``collections.abc``, see detailed specification
below.

* TypeScript provides support for user defined classes and interfaces.
Explicit implementation declaration is not required, structural subtyping
is verified statically. For example::
* TypeScript[typescript]_provides support for user defined classes and
interfaces.Explicit implementation declaration is not required,
structural subtypingis verified statically. For example::

interface LabelledItem {
label: string;
Expand All@@ -209,8 +210,8 @@ related to structural subtyping in Python and other languages:
checking without runtime implications looks reasonable, and basically
this proposal follows the same line.

* Go uses a more radical approach and makes interfaces the primary way
to provide type information. Also, assignments are used to explicitly
* Go[golang]_uses a more radical approach and makes interfaces the primary
wayto provide type information. Also, assignments are used to explicitly
ensure implementation::

type SomeInterface interface {
Expand DownExpand Up@@ -341,7 +342,7 @@ that don't have an implementation. Such syntax mirrors how methods are
defined in stub files. Abstract static methods, abstract class methods,
and abstract properties are equally supported.

To defined a protocol variable one should use the PEP 526 variable
To defined a protocol variable one should use the PEP 526[pep526]_variable
annotations in the class body. Attributes defined in the body of a method
by assignment via ``self`` considered non-protocol members. The rationale for
this is that the protocol class implementation is often not shared by
Expand DownExpand Up@@ -757,6 +758,9 @@ effects on core interpreter and standard library except in
from ``__annotations__`` to ``__subclsshook__()``.
* In the backported version of ``typing``, translate ``...`` class attribute
values to abstract properties.
* All structural subtyping checks will be performed by static type checkers,
such as ``mypy`` [mypy]_, no special support for protocol validation will
be provided at runtime.


Changes in typing module
Expand DownExpand Up@@ -785,7 +789,7 @@ what are the methods these protocols implement, and immediately recognize
the corresponding runtime protocol counterpart.
Practically, few changes will be needed in ``typing`` since some of these
classes already behave the necessary way at runtime. Most of these will need
to be updated only in the corresponding ``typeshed``stub.
to be updated only in the corresponding ``typeshed``stubs [typeshed]_.

All other concrete generic classes such as ``List``, ``Set``, ``IO``,
``Deque``, etc are sufficiently complex that it makes sense to keep
Expand DownExpand Up@@ -873,9 +877,9 @@ complicate both the concept and the implementation.
Use assignments to check explicitly that a class implements a protocol
----------------------------------------------------------------------

In Go language[golang]the explicit checks for implementation are performed
via dummy assignments. Such way is also possible with the current proposal.
Example::
In Go language the explicit checks for implementation are performed
via dummy assignments [golang]_. Such way is also possible with the current
proposal.Example::

class A:
def __len__(self) -> float:
Expand DownExpand Up@@ -909,9 +913,6 @@ References
.. [pep483]
https://www.python.org/dev/peps/pep-0483/

.. [pep526]
https://www.python.org/dev/peps/pep-0526/

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

Expand All@@ -927,6 +928,9 @@ References
.. [golang]
https://golang.org/doc/effective_go.html#interfaces_and_types

.. [pep526]
https://www.python.org/dev/peps/pep-0526/

.. [typeshed]
https://github.com/python/typeshed/

Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp