Getting typed: NumPy hits 1.20, presents Python devs with lots to play with

Getting typed: NumPy hits 1.20, presents Python devs with lots to play with

NumPy users who thought that the scientific computing library could do well with proper type annotations are in luck: version 1.20 has just landed, is largely typed and even comes with a new module with aliases for teams looking to add annotations to their own code.

The new submodule is called numpy.typing and can be imported at runtime. It contains ArrayLike and DtypeLike, which are meant to cover objects that can be coerced to an array or dtype respectively. Developers might still run into irregularities since work on the function annotations is ongoing, but the release notes mention that improvements “can be expected pending feedback from users.”

Those wanting to help the projects via comments find more than enough opportunities to do so with v1.20, since the NumPy team has tackled various issues in the release. Amongst other things there has been some preliminary work to simplify the process of extending dtypes with the project looking for feedback on the changed dtype and casting implementations. 

Execution speed was considered as well, so there are more ufuncs using SIMD now in a bid to accelerate common functions and allow a wider use of modern features on all sorts of different hardware.

In NumPy 1.20, the library’s functions mean, std, and var learned the new keyword argument where to limit their scope in the methods of numpy.ndarray or numpy directly. It is also available to the all and any functions, so that only specified elements or subaxes of an array are considered in a boolean evaluation. Testing should have become a bit easier, since there’s now a –mypy option to runtests which is promised to take care of setting things up properly.

Smaller improvements include mathematical expressions of the polynomial for all types in numpy.polynomial, a more readable output for object arrays with repr containing new lines, and the new capability in concatenate to provide an output dtype.

Users of the C API should be aware that the PyArray_DescrCheck macro was modified, which means that code compiled against earlier versions of the API will be incompatible with the new release. To work around that, developers are asked to either “manually inline the macro by replacing it with the new definition” or compile against v1.16.6 to grant support for all versions starting with the 1.16 release.

Another potential for failure connected to the API is the change in size of the PyArrayObject and PyVoidScalarObject structures. Programmers depending on a fixed structure size for a project should therefore check the documentation to see if there’s a way to adjust their code and get in touch with the NumPy team for help. 

It’s worth noting that NumPy 1.20 has stopped working with Python 3.6, so only versions 3.7 to 3.9 are supported, and includes various deprecations, such as using aliases for builtin types or passing shape=None to functions to functions that require a shape argument. More details regarding the new release can be found in the project’s documentation.