I have a function that merge two list of either int or str. There cannot be a situation when the two list are being of different types.
it is defined by the following code:
AddableList = ...
def add_arrays(array: AddableList, array2: AddableList) -> AddableList:
if len(array) != len(array2):
raise ValueError
return [a + b for a, b in zip(array, array2)]
When typing AddableList, using List[int]
mypy: Success: no issues
When typing AddableList, using List[str]
mypy: Success: no issues
However, mypy will return the following error
error: Unsupported operand types for + ("int" and "str")
error: Unsupported operand types for + ("str" and "int")
note: Both left and right operands are unions
Found 2 errors in 1 file (checked 333 source files)
when using typing correctly the lists with AddableList = List[Union[int, str]]
Finally, when trying to type AddableList to Union[List[int], List[str]], mypy errors with:
error: Unsupported left operand type for + ("object")
What typing should i use to resolve this issue?