

Stronger types for Python - andrewcooke
http://www.acooke.org/pytyp/pytyp.html#strong-types

======
andrewcooke
Thought people might find this amusing / interesting (wish I had written it
for April 1... although it does work). It's a spin-off from the use of type
annotations to guide the interpretation of JSON data (Pytyp will marshal
JSON/YAML data into Python classes, which is useful if you're using JSON to
talk to some kind of service).

[Edit: Optional, product and sum types are supported, along with simple
polymorphism. There's currently no type variables and no dispatch by type -
although I think both could be added]

~~~
masklinn
It's really cool that optional, product and sum types are supported.

Does it work correctly with abstract base classes as well? (I'm expecting yes,
just checking).

Could it work with structural types as well? That would strike me as "more
pythonic", and I've always been surprised that no Python type-annotation
scheme can handle structural specs (though abcs are a step in the right
direction).

~~~
andrewcooke
Thanks. I assume it works with ABCs, but haven't actually tried it (I ran out
of time and wanted to publish something as a first release this weekend).

Structural types would make more sense for general use, but not for what the
larger library was actually intended for (I think) so I never really looked in
that direction (and I don't have any ideas on how to extend it to include
them, either).

I do have an idea for a kind of type matching across sum types which will be
quite neat if it works...

------
ubernostrum
Except these aren't "strong" types. Python has those. These are pretend-static
types.

~~~
andrewcooke
Sure; in the docs I call them "runtime verification of type annotations".

------
rubergly
I'm a university student, and as a thesis project I've been working on a
static type-checker for Python. I'm interested to see if people think this
would be of practical good (my advisor and I certainly think it could be).

~~~
m0th87
I would be extremely excited about a static type checker, and given how
favorably Python is seen in this community, plenty of others would be here as
well. So please do post when you have a finished product!

I'm sure you've seen these already, but if not, they would be highly relevant:

* GvR did a commentary on some of the challenges that would arise with an optional type system: <http://www.artima.com/weblogs/viewpost.jsp?thread=85551>

* Python 3 included some syntactic changes precisely to support things like type checking: <http://www.python.org/dev/peps/pep-3107/>

~~~
rubergly
I indeed have read Rossum's blog posts, but I haven't been following Python 3
at all, and only found out about annotations after looking at Pytyp, so that's
definitely interesting and helpful, thanks.

------
riobard
If type annotations are fully respected that programmers code it like a
statically-typed language (no type change for a variable in scope), will it be
possible to AOT compile it?

~~~
andrewcooke
You could, but it's not really worth it - you'd end up with a pretty boring
language. Also, JITs (in particular PyPy) should be able to get decent
performance in the near future without asking the programmer to do anything
unusual. In simple terms they assume that types are whatever they find and
compile for that (occasionally the assumption is wrong, and they have to
correct and do something else, but most of the time you get good performance
just by pretending the program was written with static types, even though it
wasn't). Obviously it's more complex than that, but you get the idea (the hard
part is largely reducing the number of times you need to check you're
assumptions are still OK)

