(Related, but not duplicated: How to annotate attribute that can be implemented as property?)
I want to create a Protocol
, in which a field can be implemented by both a simple type and property. For example:
class P(Protocol):
v: int
@dataclass
class Foo(P):
v: int
class Bar(P):
@property
def v(self) -> int: # ERROR
return
But the code above doesn't type check. How should I fix it?
Note: I want to solve this issue without rewriting Foo
and Bar
, because Foo
and Bar
are not what I implemented.
According to this issue the below code is not a solution because read-only property
and a simple member have subtly different semantics.
class P(Protocol):
@property
def v(self) -> int: # declare as property
...
Pyright denies this Protocol
due to the difference.