[Numpy-discussion] Type annotations for NumPy

Jelle Zijlstra jelle.zijlstra at gmail.com
Sun Nov 26 10:04:16 EST 2017

2017-11-26 6:00 GMT-05:00 Kirill Balunov <kirillbalunov at gmail.com>:

> Hi!
> 2017-11-26 4:31 GMT+03:00 Juan Nunez-Iglesias <jni.soma at gmail.com>:
>> On 26 Nov 2017, 12:27 PM +1100, Nathaniel Smith <njs at pobox.com>, wrote:
>> It turns out that the PEP 484 type system is *mostly* not useful for
>> this. They're really designed for checking consistency across a large
>> code-base, not for enabling compiler speedups. For example, if you
>> annotate something as an int, that means "this object is a subclass of
>> int". This is enough to let mypy catch your mistake if you
>> accidentally pass in a float instead, but it's not enough to tell you
>> anything at all about the object's behavior -- you could make a wacky
>> int subclass that acts like a string or something.
> I have subscribed to many lists, although I am not an active participant
> in them. Nevertheless this topic of using the type annotation in their
> projects was discussed several times on all Cython-like channels (and it
> becomes much more acute now days). "Misconceptions" arise both for ordinary
> users and developers, but I have never seen anyone to write clearly why the
> application of type annotation in Cython (and similar projects) is
> impossible or not reasonable. Maybe someone close to the topic has the time
> and energy to sum up and write a brief summary of how to perceive them and
> why they should be viewed "orthogonal"?
> Maybe I'm looking too superficially at this topic. But both Mypy and
> Cython perform type checking. From the Cython point of view I do not see
> any pitfalls, type checking and type conversions are what Cython is doing
> right now during compilation (and looks at types as strictly as necessary).
> From Mypy's point of view, it's possible that it can delegate all this
> stuff, using a certain option, on a project's related type checker (which
> can be much stricter in its assumptions)
The main (perceived) difficulty is that the type systems are different. If
Cython has a list-typed argument, it wants exactly a list so it can use
specialized code for lists, but to mypy it means "list or a subclass of
list", which is not as easily optimized because the subclass may do things
differently from the base class. Similarly, to Cython an int means a C int,
and in Python it may mean an arbitrary-precision integer.

However, Cython managed to overcome the problem and actually added support
for type annotations recently; see
https://github.com/cython/cython/issues/1672 and
https://github.com/cython/cython/issues/1850. I haven't used the support
myself and there are probably still details to be worked out, but in
principle it should be possible to use both Cython and mypy on a codebase.

> With kind regards, -gdg
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at python.org
> https://mail.python.org/mailman/listinfo/numpy-discussion
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20171126/fd053c36/attachment.html>

More information about the NumPy-Discussion mailing list