
Type hinting for Python - zedpm
http://lwn.net/SubscriberLink/627418/a09d6081e42b3bc0/
======
willvarfar
Apologies to everyone tired of hearing about it, but an alternative non-
standard syntax is my own obiwan
[https://pypi.python.org/pypi/obiwan](https://pypi.python.org/pypi/obiwan)

~~~
ak217
My contribution is
[https://github.com/kislyuk/ensure](https://github.com/kislyuk/ensure)

It's fast and doesn't monkey-patch things.

~~~
mosquito_hater

        @ensure_annotations
        def f(x: int, y: float) -> float:
            return x+y
    

This looks great, I wish it was the standard

------
kibwen
HN comments from a previous submission of Guido's pre-PEP, "The Theory of Type
Hinting":
[https://news.ycombinator.com/item?id=8775638](https://news.ycombinator.com/item?id=8775638)

------
tosh
Having used optional type annotations in Dart for many many months now I
really miss them whenever I'm diving through our backend Python code base.

I don't use them heavily but they are a great way to sprinkle some clarity
over function signatures and APIs you care about and want to 'fortify'.

It's interesting to see the concept gaining traction in the Python and PHP
communities.

~~~
zodiakzz
Off-topic: Curious.. what are you making in Dart?

Personally I am in love with the language but worried about it not gaining
enough traction and ending up abandoned because of it.. that's keeping me from
using it for non-trivial side-projects.

------
eridal
This is great!

PHP is trying to provide full support for hinting (you couldn't hint a scalar
type nor returns)

I can see how most languages are slowly converging.

------
twoodfin
Gilad Bracha's been pushing for languages with optional type systems for some
time:

[https://www.google.com/webhp?q=bracha+optional+type#safe=off...](https://www.google.com/webhp?q=bracha+optional+type#safe=off&q=bracha+optional+type)

------
loftsy
The proposal for square brackets List[int] in generics is the weirdest thing.
There is a standard un-surprising way to do this and it is angle brackets
List<int>. What is the reason for forcing developers to use this jarring
syntax?

~~~
afandian
Angle-brackets may be familiar to C++, Java, C# and friends. But square-
brackets are used by Scala (and maybe others).

~~~
ajanuary
Eiffel also uses square brackets.

------
barumrho
I'm curious how this is valid Python:

    
    
      def fib(n: int) -> Iterator[int]:
    
    
      - Colon inside argument list
      - Arrow and type
    

What are they used for normally?

~~~
icebraining
They're generic Function Annotations:
[https://www.python.org/dev/peps/pep-3107/](https://www.python.org/dev/peps/pep-3107/)

------
hbrundage
Has anyone explored the potential for these hints to be a front-end to Cython,
or provide optimizations in CPython/PyPy? Static typing has big perf benefits
too right, is that included in the eventual goals?

~~~
kmod
At least for Python, there are a different set of goals for programmer-
productivity type hints (such as these) and for static-compilation type hints
(such as you would need to provide optimizations).

One particular point is Python subclasses: it makes a lot of sense to say that
a Python subclass is a subtype of the parent class, but in reality there are
no guarantees about any relationship. This means that a compiler/VM would most
likely have a hard time making use of type annotations since it would have to
have to support the user passing arbitrary subclasses with completely
different behavior.

------
suvelx
Can we get multiple dispatch too?

Or is

\- MD considered an anti-pattern these days?

\- This no longer 'hinting'

~~~
kibwen
I don't think anyone considers multidispatch to be an anti-pattern. Here's
Guido himself emulating multimethods in Python via decorators:
[http://www.artima.com/weblogs/viewpost.jsp?thread=101605](http://www.artima.com/weblogs/viewpost.jsp?thread=101605)

They're certainly a bit obscure though. Julia is the only new language that
I'm aware of that prominently features multiple dispatch as a first-class
language feature:
[http://julia.readthedocs.org/en/latest/manual/methods/](http://julia.readthedocs.org/en/latest/manual/methods/)

~~~
ptx
Nim also supports them: [http://nim-lang.org/manual.html#multi-
methods](http://nim-lang.org/manual.html#multi-methods)

------
gr3yh47
This is a horrible idea.

of any language, python could be a poster-child for duck typing done right.

python is also elegant and very easy to read.

this is pythonic -

    
    
        def foo(a, b, c):
            pass
    

this is ugly/trash -

    
    
        def foo(a, b: int, c: str) -> str:
            pass
    
    

if this goes down and eventually leads to static typing i will have even less
reason to consider using python 3.

~~~
Animats
Who modded that down? He has a good point. This is a bad idea. In particular,
"Python's static type-checking would be optional - programs can still be run
even if the static checker has complaints." is a really bad idea. It means the
type annotations can't be used for optimizations. Or we'll have code that only
runs with optimization turned off, or on some compilers.

~~~
minthd
What if:

1\. The default will be dynamic typing. But you can change that.

2\. You can specify a specific module you release as static. This will only
affect the internal parts of the module, with one exception - when you call
something of that module with the wrong type, you'll get a warning "this runs
XX times slower because of a wrong type, change..." , but when you run it with
the right type, you'll get great speedup ?

------
ctur
I look forward to this being back-ported to Python 2.x so that it might get
used in the real world.

