data-science-types
data-science-types copied to clipboard
bug in ndarray: incompatible type error
I think there is a bug in ndarray type hint.
I think that in case of np.array that has one row with only integers (type int or int[64]) and one row with at least one float (e.g. type float), then it produces an error since rows have different types so conflict like "np.ndarray[float] != np.ndarray[int]" occurs.
Reproducing code example:
# bug.py
import numpy as np
arr = np.array([[4.2, 2, 3.5], [12, 3, 6]])
and run mypy:
$ mypy bug.py
Error message:
bug.py:3: error: Argument 1 to "array" has incompatible type "List[object]"; expected "Union[List[bool], List[List[bool]], List[List[List[bool]]], List[List[List[List[bool]]]]]"
NumPy/Python/data-science-types versions information:
1.19.2 / 3.8.5 / 0.2.20
Yeah, that's a bug. I'm not sure we can fix it. Maybe we could use List[Union[int, float]].