This question is to clarify my doubts related to python typing
from typing import Union
class ParentClass:
parent_prop = 1
class ChildA(ParentClass):
child_a_prop = 2
class ChildB(ParentClass):
child_b_prop = 3
def method_body(val) -> ParentClass:
if val:
return ChildA()
else:
return ChildB()
def another_method() -> ChildA:
return method_body(True)
print(another_method().child_a_prop)
In the above piece of code, the linting tool I used is printing error as below
error: Incompatible return value type (got "ParentClass", expected "ChildA")
(where I do method_body(True))
I have also set the method_body return type as Union[ChildA, ChildB].
This will result error: Incompatible return value type (got "Union[ChildA, ChildB]", expected "ChildA")
I am looking for a better way to do this. If anyone knows the solution, your help will be very much appreciated.