I'm trying to write some basic Python code using numpy, and as someone who is pretty committed to the concept of strict type checking, I'm finding it a bit challenging.
I'm using VS Code with pylance, and my environment is somewhat version and library constrained.
One simple example:
flags = chi_squared > .5 # chi_squared is type np.ndarray
indices = chi_squared.argsort()
The first line is fine; no type warnings, the types of everything are known (even if flags is "Any"). The second line, however, gives me a type error because "the type of argsort() is partially unknown".
That's odd... but when I tracked down the numpy type stubs, I find that argsort is in fact partially undefined:
def argsort(self, axis=..., kind=..., order=...) -> typing.Any:
'a.argsort(axis=-1, kind=None, order=None)\n\n Returns the indices that would sort this array.\n\n Refer to `numpy.argsort` for full documentation.\n\n See Also\n --------\n numpy.argsort : equivalent function'
...
axis, kind, and order have no types (and returning Any is kind of lame as well... this function can really return any type?)
As a result, about half the lines in my code have cascading type errors like this, so my code is filled with # type: ignore directives.
I get that types in Python are a sort of bolt-on afterthought, and that libraries like numpy do some magical things, so I don't expect perfection, but I'm left wondering if there is a sane way to get the benefits of at least some type checking while using numpy, without littering my code with ignore statements.
What is the best way forward?