Consider a Python protocol attribute which is also annotated with a protocol. I found in that case, both mypy and Pyright report an error even when my custom datatype follows the nested protocol. For example in the code below Outer
follows the HasHasA
protocol in that it has hasa: HasA
because Inner
follows HasA
protocol.
from dataclasses import dataclass
from typing import Protocol
class HasA(Protocol):
a: int
class HasHasA(Protocol):
hasa: HasA
@dataclass
class Inner:
a: int
@dataclass
class Outer:
hasa: Inner
def func(b: HasHasA): ...
o = Outer(Inner(0))
func(o)
However, mypy shows the following error.
nested_protocol.py:22: error: Argument 1 to "func" has incompatible type "Outer"; expected "HasHasA" [arg-type]
nested_protocol.py:22: note: Following member(s) of "Outer" have conflicts:
nested_protocol.py:22: note: hasa: expected "HasA", got "Inner"
What's wrong with my code?