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_
int
Python 2 only float_
float
yes complex_
complex
yes bytes_
bytes
yes str_
str
yes bool_
bool
no datetime64_
datetime.datetime
no timedelta64
datetime.timedelta
no
So you can use npt.NDArray[np.str_]
.