🌐
Python
typing.python.org › en › latest › spec › protocol.html
Protocols — typing documentation
Type checkers should reject an isinstance() or issubclass() call, if there is an unsafe overlap between the type of the first argument and the protocol. Type checkers should be able to select a correct element from a union after a safe isinstance() or issubclass() call. For narrowing from non-union types, type checkers can use their best judgement (this is intentionally unspecified, since a precise specification would require intersection types). ... © Copyright 2021, The Python Typing Team.
🌐
Python
peps.python.org › pep-0544
PEP 544 – Protocols: Structural subtyping (static duck typing) | peps.python.org
Type hints introduced in PEP 484 can be used to specify type metadata for static type checkers and other third party tools. However, PEP 484 only specifies the semantics of nominal subtyping.
🌐
Mypy
mypy.readthedocs.io › en › stable › protocols.html
Protocols and structural subtyping - mypy 1.19.1 documentation
The collections.abc, typing and other stdlib modules define various protocol classes that correspond to common Python protocols, such as Iterable[T]. If a class defines a suitable __iter__ method, mypy understands that it implements the iterable protocol and is compatible with Iterable[T]. ...
🌐
Python
typing.python.org › en › latest › reference › protocols.html
Protocols and structural subtyping — typing documentation
Predefined protocol reference lists all protocols defined in typing and the signatures of the corresponding methods you need to define to implement each protocol.
🌐
Python
docs.python.org › 3 › library › asyncio-protocol.html
Transports and Protocols — Python 3.14.3 documentation
Most of connection oriented event loop methods (such as loop.create_connection()) usually accept a protocol_factory argument used to create a Protocol object for an accepted connection, represented by a Transport object. Such methods usually return a tuple of (transport, protocol). ... The Transports section documents asyncio BaseTransport, ReadTransport, WriteTransport, Transport, DatagramTransport, and SubprocessTransport classes.
🌐
Codefellows
codefellows.github.io › sea-python-401d4 › lectures › protocols.html
Network Protocols in Python — Python 401 2.1 documentation
Let’s first look at a few interactions in this protocol. A few words first about what you are looking at. Each interaction is line-based, each line represents one message. Messages from the Server to the Client are prefaced with S (<--). Messages from the Client to the Server are prefaced with C (-->). All lines end with the character sequence <CRLF> (\r\n). Throughout this document and others, I will be using <CRLF> to represent the combination of two ASCII escape sequences: "\r\n" Although you will see this in all of the examples you read in this documentation, you should never type it.
🌐
Auth0
auth0.com › blog › protocol-types-in-python
Protocol Types in Python 3.8
June 23, 2021 - A quick introduction to the new Protocol class in Python 3.8 and how it enables structural typing
🌐
Real Python
realpython.com › python-protocol
Python Protocols: Leveraging Structural Subtyping – Real Python
July 25, 2024 - In this tutorial, you'll learn about Python's protocols and how they can help you get the most out of using Python's type hint system and static type checkers.
Find elsewhere
🌐
Python Tutorial
pythontutorial.net › home › python oop › python protocol
Python Protocol - Python Tutorial
March 31, 2025 - To make the calculate_total() more dynamic while leveraging type hints, you can use the Protocol from the typing module. The Protocol class has been available since Python 3.8, described in PEP 544.
🌐
Andrewbrookins
andrewbrookins.com › technology › building-implicit-interfaces-in-python-with-protocol-classes
Building Implicit Interfaces in Python with Protocol Classes – Andrew Brookins
July 5, 2020 - So, why is this called a “protocol” and what kinds of things is it good for? Let’s go deeper to answer those questions. The built-in function len() works with any object that has a __len__() method. Objects don’t have to declare that they have a __len__() method or subclass any special classes to work with len(). Python programmers call this state of affairs a protocol, and the most common example is probably the iteration protocol.
🌐
Codefellows
codefellows.github.io › sea-python-401d2 › lectures › protocols.html
Network Protocols in Python — Python Dev Accelerator 2.0 documentation
Let’s first look at a few interactions in this protocol. A few words first about what you are looking at. Each interaction is line-based, each line represents one message. Messages from the Server to the Client are prefaced with S (<--). Messages from the Client to the Server are prefaced with C (-->). All lines end with the character sequence <CRLF> (\r\n). Throughout this document ...
🌐
DEV Community
dev.to › shameerchagani › what-is-a-protocol-in-python-3fl1
What is a Protocol in python? - DEV Community
July 5, 2023 - In Python, protocols refer to a concept introduced in Python 3.8 as a way to define structural typing or "duck typing" within the language. A protocol is a set of methods or attributes that an object must have in order to be considered compatible ...
🌐
mypy
mypy.readthedocs.io › en › latest › protocols.html
Protocols and structural subtyping - mypy 1.20.0+dev.17326d04fc86f03084c9167a3b75b9fab311b686 documentation
The collections.abc, typing and other stdlib modules define various protocol classes that correspond to common Python protocols, such as Iterable[T]. If a class defines a suitable __iter__ method, mypy understands that it implements the iterable protocol and is compatible with Iterable[T]. ...
🌐
Turingtaco
turingtaco.com › protocols-default-methods-inheritance-and-more
Protocols: Default Methods, Inheritance, and More
January 24, 2025 - Concrete classes TextFileHandler and SimpleDataProcessor (lines 32-41) provide specific implementations for the methods defined in the FileHandler and DataProcessor Protocols. Their instances are created and used in the read_process_write function (lines 43-45), illustrating how objects adhering to different Protocols can be handled within the same function. In this article, we explored the power of Python's Protocols.
🌐
Simon Willison
til.simonwillison.net › python › protocols
Protocols in Python | Simon Willison’s TILs
July 26, 2023 - That reveal_type(row) line will raise an error if you run the code using python and not mypy. The fix for that looks like this: from typing import TYPE_CHECKING ... if TYPE_CHECKING: reveal_type(obj) PEP 544 – Protocols: Structural subtyping (static duck typing)
🌐
Towards Data Science
towardsdatascience.com › home › latest › protocols in python
Protocols in Python | Towards Data Science
March 6, 2025 - With the introduction of protocols, Python obtained an elegant way of defining "interfaces" and allowing mypy to check adherence to these: in a more Pythonic style, protocols allow specifying which attributes / functions classes need to implement, and then allowing the usage of all such classes as subtypes of the protocol.
🌐
Readthedocs
ref.readthedocs.io › en › latest › understanding_python › interfaces › existing_protocols.html
Python’s Native Protocols — Reference Pages 0.2 documentation
August 27, 2024 - Python’s Native Protocols · Edit on Bitbucket · The only validation for these is by EAFP (duck-typing). Many are now encapsulated by ABCs: http://docs.python.org/dev/library/collections.abc.html · Most of these are invoked in a special way when using builtins: http://docs.python.org/dev/reference/datamodel.html#special-method-names ·
🌐
Mimo
mimo.org › glossary › python › protocol
Python Protocol: Syntax, Usage, and Examples
A protocol defines a set of methods and attributes that a type must have. It lets you write flexible, type-safe code without relying on inheritance.
Top answer
1 of 8
65

New in Python 3.8:

Some of the benefits of interfaces and protocols are type hinting during the development process using tools built into IDEs and static type analysis for detection of errors before runtime. This way, a static analysis tool can tell you when you check your code if you're trying to access any members that are not defined on an object, instead of only finding out at runtime.

The typing.Protocol class was added to Python 3.8 as a mechanism for "structural subtyping." The power behind this is that it can be used as an implicit base class. That is, any class that has members that match the Protocol's defined members is considered to be a subclass of it for purposes of static type analysis.

The basic example given in PEP 544 shows how this can be used.

Copyfrom typing import Protocol

class SupportsClose(Protocol):
    def close(self) -> None:
        # ...

class Resource:
    # ...
    def close(self) -> None:
        self.file.close()
        self.lock.release()

def close_all(things: Iterable[SupportsClose]) -> None:
    for thing in things:
        thing.close()

file = open('foo.txt')
resource = Resource()
close_all([file, resource])  # OK!
close_all([1])     # Error: 'int' has no 'close' method

Note: The typing-extensions package backports typing.Protocol for Python 3.5+.

2 of 8
21

In short, you probably don't need to worry about it at all. Since Python uses duck typing - see also the Wikipedia article for a broader definition - if an object has the right methods, it will simply work, otherwise exceptions will be raised.

You could possibly have a Piece base class with some methods throwing NotImplementedError to indicate they need to be re-implemented:

Copyclass Piece(object):

    def move(<args>):
        raise NotImplementedError(optional_error_message) 

class Queen(Piece):

    def move(<args>):
        # Specific implementation for the Queen's movements

# Calling Queen().move(<args>) will work as intended but 

class Knight(Piece):
    pass

# Knight().move() will raise a NotImplementedError

Alternatively, you could explicitly validate an object you receive to make sure it has all the right methods, or that it is a subclass of Piece by using isinstance or isubclass. Note that checking the type may not be considered "Pythonic" by some and using the NotImplementedError approach or the abc module - as mentioned in this very good answer - could be preferable.

Your factory just has to produce instances of objects having the right methods on them.