Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using type hints to translate Python to Cython

Type Hints now are available in Python 3.5 version. In the specification (PEP 484) the goals (and the non-goals) are exposed clearly:

Rationale and Goals

This PEP aims to provide a standard syntax for type annotations, opening up Python code to easier static analysis and refactoring, potential runtime type checking, and (perhaps, in some contexts) code generation utilizing type information. [...]

Of these goals, static analysis is the most important.

Non-goals

Using type hints for performance optimizations is left as an exercise for the reader.

On the other hand, Cython has been using for a long time static syntax to improve performance. Usually, people rewrite some pieces of their code with Cython syntax, compile them, and then import them back as independent modules. It's a painful job do all that on a large code base. But the worst part is that even when you follow correctly all the above steps, you don't have any guarantee that you'll have a real improvement (because of compatibility problems that might be caused if you are using some modules).

Would be a difficult task write a tool that uses this new type hints things scattered in the code to automatically translate them to Cython syntax and possibly do the rest of the job (compile them into modules and import all them back)? It would be possible, therefore, to share the same language syntax in all the code base.

Theoretically, it's possible to write a tool like that, but I'm not sure if be worth (in terms of complexity to write it and the real improvement that would be yield).

Thanks.

like image 640
GustavoIP Avatar asked Jun 24 '16 17:06

GustavoIP


1 Answers

Someone else just asked about 484 and Cython, PEP-484 Type Annotations with own types, and I responded with a thread from a couple of months back about 484 and numpy.

I have doubts about the suitability of this topic for Stackoverflow. It's a research topic,not a 'how do I solve this coding problem' question.

Based on limited reading, the type-hints in 484 are preliminary, and any use is limited to the code checker developed by the 484 authors. Py3 has had annotations for a long time, but I've seen very few examples of code that includes them. Certainly not in the numpy code that I focus on here.

Another point is that cython and numpy (and numba) are used with Py2 just as much, if not more, than Py3. So the latest bells-n-whistles in Py 3.5 are generally ignored by these users. The @ operator is the only recent addition that numpy users value.

You are welcome respond, but I may nominate this question for closure based on it being a duplicate or off topic.

The typing module is being developed at https://github.com/python/typing

mypy is the type checker based on 484, https://github.com/python/mypy (funny, ~/mypy is the directory where I put all my SO testing scripts.)

That's where cutting edge Python type checking work is being done, not here.

like image 169
hpaulj Avatar answered Sep 21 '22 16:09

hpaulj