I have defined the following classes:
@dataclass
class Var:
name: str
@dataclass
class Val:
value: int
@dataclass
class Op:
operation: str
left: 'Node'
right: 'Node'
and created a union type of these 3
Node = Var | Val | Op
Now I want to check using structural pattern matching the following cases:
match node:
case Var(x):
return x
case Val(i):
return str(i)
case Op(operation, (left), (right)):
return "(" + node_to_str(left) + " " + operation + " " + node_to_str(right) + ")"
The problem is, that, in the last case, left and right could be None, giving me an error. Hence, I would like to put the constraint on left and right that they must be a Node type:
case Op(operation, Node(left), Node(right)):
return "(" + node_to_str(left) + " " + operation + " " + node_to_str(right) + ")"
However, using this approach, I get the error: called match pattern must be a type. What am I doing wrong?