# Combining protocols with the \`&\` operator

**URL:** <https://discuss.python.org/t/combining-protocols-with-the-operator/43321>\
**Category:** Ideas\
**Tags:** typing\
**Created:** [January 16, 2024, 2:06pm UTC](https://discuss.python.org/t/combining-protocols-with-the-operator/43321 "2024-01-16T14:06:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nachomaiz](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/nachomaiz/32/17224_2.png) [@nachomaiz](https://discuss.python.org/u/nachomaiz)\
**Post date:** [January 16, 2024, 2:06pm UTC](https://discuss.python.org/t/combining-protocols-with-the-operator/43321/1 "2024-01-16T14:06:01Z")

</div>

Would it be of interest to anyone else to be able to combine Protocols with the `&` operator?

This could help address typing issues once a project embraces dependency inversion and starts using small protocols to define interfaces across the entire project.

When defining, for example, a Protocol that implements ` __len__ ` and ` __getitem__ `, one must currently define your own protocol or inherit from other protocols:

```py
from typing import Protocol, TypeVar

T_co = TypeVar("T_co", covariant=True)

class SizedCollection(Generic[T_co], Protocol):
    def __len__ (self) -> int: ...
    def __getitem__ (self, __key) -> T_co: ...

```

```py
from typing import Sized, Protocol, Generic, TypeVar

T_co = TypeVar("T_co", covariant=True)

class SizedCollection(Sized, Generic[T_co], Protocol):
    def __getitem__ (self, __key) -> T_co: ...

```

This idea would enable new protocols to be defined as:

```py
from typing import Sized, GetItemProtocol

SizedCollection = Sized & GetItemProtocol

def func(obj: Sized & GetItemProtocol):
    ...

```

The type checker would then know that `SizedCollection` implements the interface defined in `Sized` AND the hypothetical `GetItemProtocol`, with the order of operands determining inheritance rules.

It could play well with the new generic syntax:

```py
type SizedCollection[T] = Sized[T] & GetItemProtocol[T]

```

It also plays well in contrast with the current use of the union `|` operator to define types, and seems intuitive based on the rest of the typing syntax in my view.

Thoughts? Would love to hear the community’s opinions. 🙂

---

<div class="post-metadata">

**Author:** ![Daverball](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/daverball/32/15931_2.png) [@Daverball](https://discuss.python.org/u/Daverball)\
**Post date:** [January 16, 2024, 2:28pm UTC](https://discuss.python.org/t/combining-protocols-with-the-operator/43321/2 "2024-01-16T14:28:19Z")

</div>

A more general PEP for type intersections is currently being drafted \[1\], I don’t think it makes sense to implement protocol intersections in a separate step, even if it is easier than a more general type intersection.

In the meantime there’s this mypy plugin for Protocol intersections: [typing-protocol-intersection · PyPI](https://pypi.org/project/typing-protocol-intersection/)

* * *

1. 

> **[GitHub - CarliJoy/intersection\_examples: Python Typing Intersection examples](https://github.com/CarliJoy/intersection_examples)**
>
> Python Typing Intersection examples. Contribute to CarliJoy/intersection\_examples development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![nachomaiz](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/nachomaiz/32/17224_2.png) [@nachomaiz](https://discuss.python.org/u/nachomaiz)\
**Post date:** [January 16, 2024, 3:15pm UTC](https://discuss.python.org/t/combining-protocols-with-the-operator/43321/3 "2024-01-16T15:15:32Z")

</div>

Oh, that’s really great!

I looked through this page and the GitHub issues and didn’t find anything on a first pass.

It looks like they are also proposing using `A & B` for defining intersections, so great timing then! 🙂

Will be looking forward to developments from the PEP proposal then.

Thanks!
