Mypy – static type checking for Python 3
github.com
github.com
https://mail.python.org/pipermail/python-ideas/2014-August/0...
https://mail.python.org/pipermail/python-ideas/2014-August/0...
Recommended reading for those interested in the topic.
Python should take example on Typescript. It's light and smart. And that's enough to cover 99% of the cases.
Just let me specify that function foo's first parameter is an array of "stuff", and i'll be the happiest man. Leave the complicated cases for future versions.
https://github.com/kislyuk/ensure
(Having optional static type checks in the core interpreter would be nice, though Alex Gaynor's concerns linked in another comment are very valid.)
Although it seems that MyPy has done a lot of progress since, most notably adopting Python 3 syntax for type annotations.
Doing iOS (with Swift) for a while has made me dislike not having static typing.
You're right. None of this would provide any value for PyPy.
https://mail.python.org/pipermail/python-ideas/2014-August/0...
Even if you write a program that you can provably show will never have any runtime types that differ from compile time types, Pypy can't assume that will be the case with every program that it tries to run. I don't have any specific knowledge of the optimizations that Pypy runs, but if one of the core contributors of Pypy says it can't be done, I'm inclined to believe him.
Therefore your claim that there is "_never_ a guarantee" is simply false. I was asking: what if you DO want PyPy to assume that your types are correct, why couldn't that help?
EDIT: Here is a trivial example of a program that type checks at compile time but doesn't present the same types at runtime. Obviously you would never do this in production code, but the same effect could be accomplished simply by doing an `import somemodule`.
It is unfortunate that such dynamicity is the default in Python, when in reality it is rarely needed. So it would be advantageous to be able to tell the optimization system: this function is not going to change dynamically (on an opt-in basis).
v = 1234
Then, PyPy would crash or behave erratically since its assumptions about the value's type are false.
> v = 1234
Surely at least this could be caught by a static type checker? If `v` is annotated to be of type `str`, assigning it a constant int should be a type error, though I'm sure Python provides more indirect ways of setting `v` that would be harder to catch statically (like globals()['v'] = ...).
But your comment raises an interesting question: are the annotations MyPy is relying on (static) type annotations, or value annotations?
I tend to think of the difference between static and dynamic typing as whether types attach to expressions or to values. If annotations only attach to the value to which a variable is initially bound in some context, I can see how they really wouldn't be much help in this scenario, since `v = 1234` is simply rebinding `v` to a value of a different type, and a previous annotation wouldn't preclude that. But if annotations attach to expressions, then it would be inconsistent to bind `v` to a value like 1234, since the constant "1234" is of a different type than `v` is.
Indeed, if you consider type annotation as just annotation and nothing else. But at least you "could" imagine an option to have pypy optimize based on them.
In many (including many of the most popular) statically-typed (and not optionally so) -- particularly OO -- languages, the compile-time types in many declarations are type bounds, but not assured to be the exact runtime types. So I hardly see that it is problematic that this would be the case with optional type annotations in a dynamic language.
PyPy can of course take that mindset, forking the language into some typed python dialect which, for well-typed programs, returns the same stuff as normal Python. Actually, you may be surprised to know that this language has already been completed (!) and a significant program has been written in it (!!). That significant program is called PyPy (wow so meta), and the language it's written in, which alters Python so that static type inference is possible, is called RPython.
But assuming that PyPy doesn't force everyone to write RPython, static type inference cannot be done easily in Python and static type annotations are strictly advisory. Given this, PyPy will keep doing its just-in-time thing. Then, we're compiling while the code is executing. But then what help is a type hint? It's run-time. We have the argument; we know what Python type it is supposed to be. The problems which PyPy has to solve are things like, "can I optimize this Python `int` into a single 64-bit word?" and those problems are not addressed at all by the type hint.
Gradual typing is relevant in this regard, which makes it possible to combine static and dynamic types and do type inference. See e.g. https://github.com/tomprimozic/type-systems/tree/master/grad...
Now of course PyPy may hold the position that it's going to eschew static types on principle, but then it's a matter of ideology, not a technical reason, which causes these type annotations to "never have any value for PyPy".