Skip to content

Commit 86ebd53

Browse files
Clarify protocol vs Protocol
1 parent 25bfb64 commit 86ebd53

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

designs/serialization.md

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,44 @@ based on information from a Smithy model.
1919
feed well-formed data from any source into a protocol and have it transform
2020
that data with no side-effects.
2121

22+
## Terminology - `Protocol` vs protcol
23+
24+
In Smithy, a "protocol" is a method of communicating with a service over a
25+
particular transport using a particular format. For example, the
26+
`aws.protocols#RestJson1` protocol is a protocol that communicates over the an
27+
HTTP transport that makes use of REST bindings and formats structured HTTP
28+
payloads in JSON.
29+
30+
In Python, a
31+
[`Protocol`](https://typing.readthedocs.io/en/latest/spec/protocol.html#protocols)
32+
is a type that is used to define structural subtyping. For example, the
33+
following shows a `Protocol` and two valid implementations of it:
34+
35+
```python
36+
class ExampleProtocol(Protocol):
37+
def greet(self, name: str) -> str:
38+
return f"Hello {name}!"
39+
40+
class ExplicitImplementation(ExampleProtocol):
41+
pass
42+
43+
class ImplicitImplementation:
44+
def greet(self, name: str) -> str:
45+
return f"Good day to you {name}."
46+
```
47+
48+
Since this is *structural* subtyping, it isn't required that implmentations
49+
actual inheret from the `Protocol` or otherwise declare that they're
50+
implementing it. But they *can* to make it more explicit or to inherit a default
51+
implementation. The `Protocol` class itself cannot be instantiated, however.
52+
53+
This overlapping of terms clearly can cause confusion. To hopefully avoid that,
54+
implementations of Python's `Protocol` type will referred to using the literal
55+
`Protocol` or the general term "interface". (A protocol *isn't* quite the same
56+
thing as an interface in other programming languages, but for our purposes it's
57+
close enough.) Smithy protocols will be referred to simply as "protocol"s or by
58+
their specific protocol names (e.g. restJson1).
59+
2260
## Schemas
2361

2462
The basic building block of Smithy is the "shape", a representation of data of a
@@ -108,7 +146,7 @@ then be the method by which all serialization is performed.
108146
Using open interfaces in this way allows for great flexibility in the generated
109147
Python code, which will be discussed more later.
110148

111-
In Python, these interfaces will be represented as `Protocol`s, as shown below:
149+
In Python these interfaces will be represented as shown below:
112150

113151
```python
114152
@runtime_checkable

0 commit comments

Comments
 (0)