On Wed, Aug 26, 2020 at 11:20 AM Christopher Barker <pythonchb@gmail.com> wrote:


On Wed, Aug 26, 2020 at 9:02 AM Guido van Rossum 
Mypy includes an experimental compiler from typed Python to C that actually does what the OP is looking for.


There is also Cython, which is almost exactly what the OP is looking for.

And certainly more mature.

(I’m curious about how mypyc is different than Cython (other than syntax) and I’ve lost track, but I thought Cython was looking into using standard type hint notation.

IIUC Cython (even if they were to adopt annotations) is not meant to be a strict subset of Python -- almost no Cython module is valid Python. OTOH mypyc has this as a goal -- you write annotated Python, and you test it using the Python interpreter, and then when you need more speed, mypyc can translate it to run faster. But you can still execute the mypyc input with the regular interpreter, and it will have the same semantics. (There are situations where mypyc doesn't handle certain dynamic constructs, but there are no cases where code can only be run by mypyc.)
 
But there’s a trick: type hints can be (and often are) duck typed. That is, they might specify a Sequence of Numbers, which doesn’t provide enough information to compile down to fully performant code. 

In fact, when type hinting was standardized, it was explicitly said that that was not the goal.

Right. And mypyc is experimental. But type hints *are* useful for compilers (see also TorchScript).
 
--
--Guido van Rossum (python.org/~guido)