๐ŸŒ
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.
๐ŸŒ
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.
Discussions

What to use in replacement of an interface/protocol in python - Stack Overflow
I am making a chess game and wanted ... interface/protocol. Python does not have those in the language, so what am I supposed to use? I read a bit about factories, but I'm not sure how they would help. Thanks in advance! ... What would be the point of interfaces in Python, as it could not force you to implement all the methods? ... Interfaces are important for languages that are statically typed and you want ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Interfaces with Protocols: why not ditch ABC for good?
Whereas with Protocols it's gonna be ( good tutorial ): I think that is not a good example of how to write programs. What he did by having protocols I would have done by using mixins. The way that I see objects is that they have various capabilities that can be mixed in. multiple inheritance in python would have been a much better way to implement that example in my opinion. I would also say that the author of this tutorial needs to learn a thing or 2 about an inversion of control and dependency injection. The author basically sets up a straw man problem and then solves his straw man problem. He had no business creating instances of the object outside of the class itself. If he had simply called a constructor methods within the classes then the other class wouldn't have been attempting to make instances of those other classes. More on reddit.com
๐ŸŒ r/Python
34
63
January 22, 2023
Protocols vs Abstract Base Classes in Python
I'm going to add that performance for protocols is bad when doing an instance check. It's O(n) where n is the size of the interface, and they're not cheap checks. For example, I improved performance in ReactPy by 50% by removing a single isinstance check against a protocol that was done every render. More on reddit.com
๐ŸŒ r/Python
31
121
December 3, 2024
How to typehint any object belonging to a subclass belonging to a parent class
Type[Shape] is deprecated only because you should now use the built-in type function instead: type[Shape]. But neither of those do what you want; they're for asserting that the function expects a class object, rather than (as usual) an instance of a class. I don't think there's any way to say that you want only subclasses of Shape. Perhaps the best you can do would be to specify the classes you want explicitly: def find_perimeter(shape: Square | Triangle) -> int: More on reddit.com
๐ŸŒ r/learnpython
7
5
November 10, 2023
๐ŸŒ
Mypy
mypy.readthedocs.io โ€บ en โ€บ stable โ€บ protocols.html
Protocols and structural subtyping - mypy 1.19.1 documentation
This is a type for objects that support the in operator. ... See also Container. def __len__(self) -> int def __iter__(self) -> Iterator[T] def __contains__(self, x: object) -> bool ยท See also Collection. These protocols are typically only useful with a single standard library function or class.
๐ŸŒ
Medium
medium.com โ€บ @commbigo โ€บ python-typing-protocol-c2f6a60c0ac6
Python typing โ€” Protocol
December 28, 2023 - from typing import Protocol class Job(Protocol): def run(self): print("run the job") class Job1: pass def run_job(job: Job): job.run() run_job(Job1()) For the code above, static type checking will catch the error โ€œArgument 1 to โ€œrun_jobโ€ ...
๐ŸŒ
Python
peps.python.org โ€บ pep-0544
PEP 544 โ€“ Protocols: Structural subtyping (static duck typing) | peps.python.org
March 5, 2017 - To define these in a manner compatible with older versions of Python one can use properties. Properties can be settable and/or abstract if needed: class Foo(Protocol): @property def c(self) -> int: return 42 # Default value can be provided for property... @abstractproperty def d(self) -> int: # ... or it can be abstract return 0 ยท Also function type comments can be used as per PEP 484 (for example to provide compatibility with Python 2).
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ library โ€บ typing.html
typing โ€” Support for type hints
3 weeks ago - In code that needs to be compatible with Python 3.11 or older, generic Protocols can be written as follows: T = TypeVar("T") class GenProto(Protocol[T]): def meth(self) -> T: ...
๐ŸŒ
Sarahabd
sarahabd.com โ€บ sarah abderemane's website โ€บ til โ€บ python protocol typing
TIL: python Protocol and typing
December 18, 2024 - There a multiple protocols in python but the one Iโ€™m talking about is a way to define structural typing. Some of you will also call that implicit interface. We can use the Protocol class from the typing module. To not confuse you even more, I will explain that with a simple image. If we consider you as a journalist and you are writing tech articles, week notes and many type ...
๐ŸŒ
Python
typing.python.org โ€บ en โ€บ latest โ€บ reference โ€บ protocols.html
Protocols and structural subtyping โ€” typing documentation
Protocols can be recursive (self-referential) and mutually recursive. This is useful for declaring abstract recursive collections such as trees and linked lists: from typing import TypeVar, Optional, Protocol class TreeLike(Protocol): value: int @property def left(self) -> Optional['TreeLike']: ...
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.
๐ŸŒ
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
๐ŸŒ
Mimo
mimo.org โ€บ glossary โ€บ python โ€บ protocol
Python Protocol: Syntax, Usage, and Examples
Start your coding journey with Python. Learn basics, data types, control flow, and more ... Protocols live in the typing module (or typing_extensions in older versions).
๐ŸŒ
Pybites
pybit.es โ€บ articles โ€บ typing-protocol-abc-alternative
Leveraging typing.Protocol: Faster Error Detection And Beyond Inheritance โ€“ Pybites
February 9, 2024 - Introduction Two weeks ago I wrote an article about ABCs and interface enforcement. Shortly after I learned that you can do this as well with protocols. Python 3.8 introduced quite a groundbreaking feature that further advanced the language's capabilities in type checking: the typing.Protocol which allows Python developers to define and enforce interface contracts inโ€ฆ
๐ŸŒ
Xebia
xebia.com โ€บ home โ€บ blog โ€บ protocols in python: why you need them
Protocols In Python: Why You Need Them | Xebia
July 25, 2022 - We now have the best of both worlds: static type checking of dynamic types. The protocol class implementation defines rules and guidelines for implementing protocol methods within a class, including the use of variable annotations. Python Protocol class variables are defined within the class body of a protocol, and there is a distinction between protocol class variables and protocol instance variables.
๐ŸŒ
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 - Python 3.8โ€™s protocol classes make protocols more explicit. In effect, protocols become more like interfaces in Go. ... Then you can use the protocol class as a type annotation and mypy will check that an object implements the protocol.
๐ŸŒ
Towards Data Science
towardsdatascience.com โ€บ home โ€บ latest โ€บ protocols in python
Protocols in Python | Towards Data Science
September 29, 2025 - Protocols are an alternative to abstract base classes (ABC), and allow structural subtyping โ€“ checking whether two classes are compatible based on available attributes and functions alone. In this post weโ€™ll go into the details about this and show how to use protocols using practical examples. ... Let us begin by discussing how Python is typed.
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.

๐ŸŒ
DEV Community
dev.to โ€บ shameerchagani โ€บ what-is-a-protocol-in-python-3fl1
What is a Protocol in python? - DEV Community
July 5, 2023 - To conclude with,Protocols provide a way to define structural typing in Python, allowing you to create interfaces without the need for explicit inheritance. They enable type checkers to verify that objects adhere to the defined protocols, enhancing ...
๐ŸŒ
CodeRivers
coderivers.org โ€บ blog โ€บ python-protocol
Python Protocols: Unveiling the Magic of Duck Typing and Beyond - CodeRivers
January 24, 2025 - They enable duck typing, allowing objects to be used based on their behavior rather than their specific class type. By understanding the fundamental concepts, usage methods, common practices, and best practices of protocols, you can write more robust, reusable, and maintainable Python code.
๐ŸŒ
Adam Johnson
adamj.eu โ€บ tech โ€บ 2021 โ€บ 05 โ€บ 18 โ€บ python-type-hints-duck-typing-with-protocol
Python type hints: duck typing with Protocol - Adam Johnson
May 18, 2021 - We use class syntax to define a protocol, and add relevant members in the body with type hints. The methods will never be executed, so itโ€™s standard to use Pythonโ€™s ellipsis symbol for their bodies.
๐ŸŒ
Real Python
realpython.com โ€บ python-type-checking
Python Type Checking (Guide) โ€“ Real Python
July 15, 2024 - In this guide, you'll look at Python type checking. Traditionally, types have been handled by the Python interpreter in a flexible but implicit way. Recent versions of Python allow you to specify explicit type hints that can be used by different tools to help you develop your code more efficiently.