I have an interface specifying a position
member. Because I want it to be agnostic to whether the member is implemented as a @property
or a direct attribute, I annotate the type as a Union of the two
class Moveable(Protocol):
position: property or Tuple[int, int]
entity: Moveable = Player() # Player implements `.position` as a getter
This works. However, switching the types around results in a Pylance error.
class Moveable(Protocol):
position: Tuple[int, int] or property
(class) Player()
Expression of type "Player" cannot be assigned to declared type "Moveable"
"Player" is incompatible with protocol "Moveable"
"position" is invariant because it is mutable
"position" is an incompatible type
"property" is incompatible with "Tuple[int, int]"PylancereportGeneralTypeIssues
I'm not seeing why the order would matter, as (P or Q) <-> (Q or P)
. This violates the Commutative Law
Instead of or
, use Union[]
or |