I'm using a numpy array to store pairs of strings in order to take advantage of numpy array's memory view capability when slicing,which normal python lists don't have.
import numpy as np
import numpy.typing as npt
pairs_of_identifiers = np.array([['A11', 'A19'], ['A2', 'A6'], ...]])
random_function(pairs_of_identifiers: npt.NDArray[str]) -> None:
...
So far I have tried to annotate it as npt.NDArray[str], but I get the following pylance/mypy error:
Could not specialize type "NDArray[ScalarType@NDArray]"
Type "str" cannot be assigned to type "generic"
"str" is incompatible with "generic"
What is the correct way of annotating a NDarray of strings with mypy ?
CodePudding user response:
Refer to Built-in scalar types:
Array scalar type Related Python type Inherits? int_intPython 2 only float_floatyes complex_complexyes bytes_bytesyes str_stryes bool_boolno datetime64_datetime.datetimeno timedelta64datetime.timedeltano
So you can use npt.NDArray[np.str_].
