I want to add typehints for mathematical functions in an existing codebase. Most of the time the calculation itself is pretty easy and can be done both with pyhon numerics and numpy arrays.
However, I'm unsure how to typehinting the following function:
def something(a, b):
return a + b
This is what I tried already, I verify the typehints with mypy.
from typing import TypeVar, Union
from numbers import Number
import numpy.typing as npt
import numpy as np
def something1(a:npt.ArrayLike, b: npt.ArrayLike) -> npt.ArrayLike:
return a + b #7
x1 = something1(3,4)
T2 = TypeVar('T2')
def something2(a: T2, b: T2) -> T2:
return a + b #12
x2 = something2(3,4)
T3 = TypeVar('T3', bound=Union[Number, np.ndarray])
def something3(a: T3, b: T3) -> T3:
return a + b #17
x3 = something3(3,4)
T4 = TypeVar('T4', bound=np.ndarray)
def something4(a: T4, b: T4) -> T4:
return a + b # This works, but not with an python number
x4 = something4(3,4) #23
T5 = TypeVar('T5', bound=npt.ArrayLike)
def something5(a: T5, b: T5) -> T5:
return a + b #27
x5 = something5(3,4)
T6 = TypeVar('T6', bound=Number)
def something6(a: T6, b: T6) -> T6:
return a + b #32
x6 = something6(3,4) #33
mypy throws several errors for every function, so I'm unsure how to proceed. This is the mypy output
runsomething.py:7: error: Unsupported operand types for + ("int" and "str")
runsomething.py:7: error: Unsupported operand types for + ("int" and "bytes")
runsomething.py:7: error: Unsupported operand types for + ("int" and "generic")
runsomething.py:7: error: Unsupported operand types for + ("int" and "Sequence[Union[int, float, complex, str, bytes, generic]]")
runsomething.py:7: error: Unsupported operand types for + ("int" and "Sequence[Sequence[Any]]")
runsomething.py:7: error: Unsupported operand types for + ("int" and "_SupportsArray")
runsomething.py:7: error: Unsupported operand types for + ("float" and "str")
runsomething.py:7: error: Unsupported operand types for + ("float" and "bytes")
runsomething.py:7: error: Unsupported operand types for + ("float" and "generic")
runsomething.py:7: error: Unsupported operand types for + ("float" and "Sequence[Union[int, float, complex, str, bytes, generic]]")
runsomething.py:7: error: Unsupported operand types for + ("float" and "Sequence[Sequence[Any]]")
runsomething.py:7: error: Unsupported operand types for + ("float" and "_SupportsArray")
runsomething.py:7: error: Unsupported operand types for + ("complex" and "str")
runsomething.py:7: error: Unsupported operand types for + ("complex" and "bytes")
runsomething.py:7: error: Unsupported operand types for + ("complex" and "generic")
runsomething.py:7: error: Unsupported operand types for + ("complex" and "Sequence[Union[int, float, complex, str, bytes, generic]]")
runsomething.py:7: error: Unsupported operand types for + ("complex" and "Sequence[Sequence[Any]]")
runsomething.py:7: error: Unsupported operand types for + ("complex" and "_SupportsArray")
runsomething.py:7: error: Unsupported operand types for + ("str" and "int")
runsomething.py:7: error: Unsupported operand types for + ("str" and "float")
runsomething.py:7: error: Unsupported operand types for + ("str" and "complex")
runsomething.py:7: error: Unsupported operand types for + ("str" and "bytes")
runsomething.py:7: error: Unsupported operand types for + ("str" and "generic")
runsomething.py:7: error: Unsupported operand types for + ("str" and "Sequence[Union[int, float, complex, str, bytes, generic]]")
runsomething.py:7: error: Unsupported operand types for + ("str" and "Sequence[Sequence[Any]]")
runsomething.py:7: error: Unsupported operand types for + ("str" and "_SupportsArray")
runsomething.py:7: error: Unsupported operand types for + ("bytes" and "int")
runsomething.py:7: error: Unsupported operand types for + ("bytes" and "float")
runsomething.py:7: error: Unsupported operand types for + ("bytes" and "complex")
runsomething.py:7: error: Unsupported operand types for + ("bytes" and "str")
runsomething.py:7: error: Unsupported operand types for + ("bytes" and "generic")
runsomething.py:7: error: Unsupported operand types for + ("bytes" and "Sequence[Union[int, float, complex, str, bytes, generic]]")
runsomething.py:7: error: Unsupported operand types for + ("bytes" and "Sequence[Sequence[Any]]")
runsomething.py:7: error: Unsupported operand types for + ("bytes" and "_SupportsArray")
runsomething.py:7: error: Unsupported operand types for + ("generic" and "int")
runsomething.py:7: error: Unsupported operand types for + ("generic" and "float")
runsomething.py:7: error: Unsupported operand types for + ("generic" and "complex")
runsomething.py:7: error: Unsupported left operand type for + ("generic")
runsomething.py:7: error: Unsupported operand types for + ("Sequence[Union[int, float, complex, str, bytes, generic]]" and "int")
runsomething.py:7: error: Unsupported operand types for + ("Sequence[Union[int, float, complex, str, bytes, generic]]" and "float")
runsomething.py:7: error: Unsupported operand types for + ("Sequence[Union[int, float, complex, str, bytes, generic]]" and "complex")
runsomething.py:7: error: Unsupported left operand type for + ("Sequence[Union[int, float, complex, str, bytes, generic]]")
runsomething.py:7: error: Unsupported operand types for + ("Sequence[Sequence[Any]]" and "int")
runsomething.py:7: error: Unsupported operand types for + ("Sequence[Sequence[Any]]" and "float")
runsomething.py:7: error: Unsupported operand types for + ("Sequence[Sequence[Any]]" and "complex")
runsomething.py:7: error: Unsupported left operand type for + ("Sequence[Sequence[Any]]")
runsomething.py:7: error: Unsupported operand types for + ("_SupportsArray" and "int")
runsomething.py:7: error: Unsupported operand types for + ("_SupportsArray" and "float")
runsomething.py:7: error: Unsupported operand types for + ("_SupportsArray" and "complex")
runsomething.py:7: error: Unsupported left operand type for + ("_SupportsArray")
runsomething.py:7: note: Both left and right operands are unions
runsomething.py:12: error: Unsupported left operand type for + ("T2")
runsomething.py:17: error: Unsupported left operand type for + (some union)
runsomething.py:17: error: Unsupported operand types for + ("ndarray" and "T3")
runsomething.py:18: error: Value of type variable "T3" of "something3" cannot be "int"
runsomething.py:23: error: Value of type variable "T4" of "something4" cannot be "int"
runsomething.py:27: error: Unsupported left operand type for + (some union)
runsomething.py:27: error: Incompatible return value type (got "Union[complex, str, bytes, Any]", expected "T5")
runsomething.py:27: error: Unsupported operand types for + ("int" and "T5")
runsomething.py:27: error: Unsupported operand types for + ("float" and "T5")
runsomething.py:27: error: Unsupported operand types for + ("complex" and "T5")
runsomething.py:27: error: Unsupported operand types for + ("str" and "T5")
runsomething.py:27: error: Unsupported operand types for + ("bytes" and "T5")
runsomething.py:32: error: Unsupported left operand type for + ("T6")
runsomething.py:33: error: Value of type variable "T6" of "something6" cannot be "int"
Found 64 errors in 1 file (checked 1 source file)
Edit:
The errors are quite clear, so I'm aware it's not allowed to add int and str types. So I don't expect a clarification about all the erros, but a solution that will work for my usecase.
However, I find something3 quite promising, but I have no clue what's the issue here.
Edit2: Added Comments for easier spotting the line numbers
Edit3: The question is not about concatenation of sequences, but about adding two numbers, add numbers to arrays and add two arrays. I just used '+' as the most simple mathematical operator.
Using Python 3.8.5, numpy 1.20.2 and mypy 0.812