How do you type hint a protocol with attrs.field?

96 Views Asked by At

I want to use attrs to use its ability evolve my class, hashing and more. But I would also like to use the protocol pattern. However since attrs does not do well with multiple inheritance, I am having trouble trying to type hint my code.

This is an example code to illustrate the structure that I would like to have. my current code omit @frozen, giving an error when I use mutate etc.

from typing import Protocol

from attrs import frozen


@frozen
class AProtocol(Protocol):
    a: int


@frozen
class BProtocol(Protocol):
    b: str


@frozen
class CProtocol(AProtocol, BProtocol, Protocol):
    ...
1

There are 1 best solutions below

0
hynek On

I'm not sure what you mean by "attrs does not do well with multiple inheritance", but what I can tell you is that making a Protocol – that's not supposed to carry anything else than API information – an attrs class (or any other code generating decorator) doesn't make sense. They're not supposed to be ever instantiated.