Hacker News new | ask | show | jobs
by eggie5 3296 days ago
types in python 3.5?! I had no idea -- that's exciting.
5 comments

This was a big contribution from Dropbox: http://blog.zulip.org/2016/10/13/static-types-in-python-oh-m...
The language itself supports annotations, without any actual typechecking. You use http://mypy-lang.org/ to get the static checking.
There is also Google's pytype: https://github.com/google/pytype
That looks like it adds types rather than checks?
It does both. I can do some inference but definitely does type checking. Here's a Pycon talk from last year about pytype: https://youtu.be/IDm_YIQihhs
It needs severely more documentation, heh. Why would I use it over mypy, which appears to have a lot more community investment, thorough docs, and is somewhat official? It also doesn't support python3.6 so it doesn't know about format strings =/
pytype might work better in some cases, because it can do inference. But yes, it needs more docs.
Note they are type annotations. They are for tooling/development only; the runtime doesn't care about the types at all.
> Note they are type annotations. They are for tooling/development only; the runtime doesn't care about the types at all.

Neither does, say, Haskell's runtime.

You say that like it's a bad thing, but that's what a static language is - types are checked at compile time and, hopefully, forgotten about at runtime.
That's not entirely true. The information from the type annotations is available at runtime. ApiStar takes advantage of this, for example.
The information from annotations is exposed to the runtime, but nothing in the runtime treats them as types or performs any type checking. The feature was introduced as a generic way to annotate functions, without being constrained to a single use case (type checking), and all the tools which actually do type-checking based on annotations are third-party.
C++ also does do any type checking at runtime. Compile time checking is all that really matters.
The CPython implementation does have a "compile" step (to produce bytecode, which is what actually gets executed, by a simple stack-based virtual machine), and does not do any type checking in that step no matter how many annotations you give it.
Hmm, Perl6 has real types, although they're completely optional.
which language's run time cares about types?
Any dynamic language - by definition, they check types at runtime.

Or, unfortunately, a lot of static languages. Any static language that allows type casting, for instance - that's the only way they can check whether a cast from a type to one of its descendants is valid (eg, in Java, casting an Object to a, say, URL).

Type information is used in JS engines to compile into native code. If type information is too vague or change you can have deoptimization[0]

[0]http://jayconrod.com/posts/54/a-tour-of-v8-crankshaft-the-op...

The types do the actually speed up execution but I find them useful in various parts of my codebase when I work on a project (for exaple when defining classes which represent more advanced and complex data types for my specific project). It help staying organized and making sure your not ambiguously passing in wrong data types. Also pycharm uses them to understand custom classes and functions you define and provide suggestions.
Anybody have experience of using this? More info: https://docs.python.org/3/library/typing.html

I've been using TypeScript a lot recently and it has had a big impact on reducing bugs and making refactoring easier so something similar for Python looks great.