[Numpy-discussion] Type annotations for NumPy

Stephan Hoyer shoyer at gmail.com
Sat Nov 25 18:12:16 EST 2017


On Sat, Nov 25, 2017 at 7:21 AM Marten van Kerkwijk <
m.h.vankerkwijk at gmail.com> wrote:

> A question of perhaps broader scope than what you were asking for, and
> more out of curiosity than anything else, but can one mix type
> annotations with others? E.g., in astropy, we have a decorator that
> looks for units in the annotations (not dissimilar from dtype, I
> guess). Could one mix annotations or does one have to stick with one
> purpose?
>

Hi Marten,

I took a look at Astropy's units decorator:
http://docs.astropy.org/en/stable/api/astropy.units.quantity_input.html

Annotations for return values that "coerce" units would be hard to make
compatible with typing, because type annotations are used to check
programs, not change runtime semantics. But in principle, I think you could
even make a physical units library that relies entirely on static type
checking for correctness, imposing almost no run-time overhead at all.
There are several examples for Haskell:
https://wiki.haskell.org/Physical_units

I don't see any obvious way to support to mixing of annotations for typing
and runtime effects in the same function, though doing so in the same
program might be possible. My guess is that the preferred way to do this
would be to use decorators for runtime changes to arguments, and keep
annotations for typing. The Python community seems to be standardizing on
using annotations for typing:
https://www.python.org/dev/peps/pep-0563/#non-typing-usage-of-annotations

Cheers,
Stephan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20171125/09b2eba4/attachment.html>


More information about the NumPy-Discussion mailing list