Has anyone implemented type hinting for the specific numpy.ndarray class?
Right now, I'm using typing.Any, but it would be nice to have something more specific.
For instance if the numpy people added a type alias for their array_like object class. Better yet, implement support at the dtype level, so that other objects would be supported, as well as ufunc.
Check out DataShape. It uses the datatypes as well as some syntax for how big the input and output arrays should be.
It looks like
typing
module was developed at:https://github.com/python/typing
The main
numpy
repository is athttps://github.com/numpy/numpy
Python bugs and commits can be tracked at
http://bugs.python.org/
The usual way of adding a feature is to fork the main repository, develop the feature till it is bomb proof, and then submit a pull request. Obviously at various points in the process you want feedback from other developers. If you can't do the development yourself, then you have to convince someone else that it is a worthwhile project.
cython
has a form of annotations, which it uses to generate efficientC
code.You referenced the
array-like
paragraph innumpy
documentation. Note itstyping
information:In other words the
numpy
developers refuse to be pinned down. They don't, or can't, describe in words what kinds of objects can or cannot be converted tonp.ndarray
.For your own functions, an annotation like
works. Of course if your function ends up calling some
numpy
function that passes its argument throughasanyarray
(as many do), such an annotation would be incomplete, since your input could be alist
, ornp.matrix
, etc.When evaluating this question and answer, pay attention to the date. 484 was a relatively new PEP back then, and code to make use of it for standard Python still in development. But it looks like the links provided are still valid.
What i did was to just define it as
So for example if you want an array of floats you can do:
a = numpy.empty(shape=[2, 2], dtype=float) # type: Dict[Tuple[int, int], float]
This is of course not exact from a documentation perspective, but for analyzing correct usage and getting proper completion with pyCharm it works great!