I’m encountering an issue with my Python code using Pyright type checker, specifically when I try to remove Any from the return type declaration.
Look/read this question first: Handling Python Type Checker Errors with Return Type Declarations (fields default values)
Error message:
Function with declared return type "int" must return value on all code paths. "None" is incompatible with "int".
What about this case? I still get the same error as above(question from stack overflow), even though my classes do not contain any None.
class Exp:
pass
@dataclass
class Const(Exp):
value: int
@dataclass
class Negate(Exp):
value: Exp
@dataclass
class Add(Exp):
value1: Exp
value2: Exp
@dataclass
class Multiply(Exp):
value1: Exp
value2: Exp
def eval_exp_old(e: Exp) -> int | Any:
match e:
case Const(i):
return i
case Negate(e2):
return -eval_exp_old(e2)
case Add(e1, e2):
return eval_exp_old(e1) + eval_exp_old(e2)
case Multiply(e1, e2):
return eval_exp_old(e1) * eval_exp_old(e2)
test_exp: Exp = Multiply(Negate(Add(Const(2), Const(2))), Const(7))
# -28
old_test = eval_exp_old(test_exp)
I appreciate any advice.