[Python-ideas] Proposal: Use mypy syntax for function annotations

Burak Arslan burak.arslan at arskom.com.tr
Wed Aug 20 17:58:52 CEST 2014


Chiming in as well (first post to python-ideas, fingers crossed!),
because Spyne (I'm the author) will have to interoperate with this
sooner or later :)

To put my opinions in context:
http://spyne.io/docs/2.10/manual/03_types.html


On 08/13/14 22:44, Guido van Rossum wrote:
> [There is no TL;DR other than the subject line. Please read the whole
> thing before replying. I do have an appendix with some motivations for
> adding type annotations at the end.]
>

Not only that, but I went through most of the thread as well :)

>
> To read up on mypy's annotation syntax, please see the mypy-lang.org
> <http://mypy-lang.org> website. Here's just one complete example, to
> give a flavor:
>
>   from typing import List, Dict
>
>   def word_count(input: List[str]) -> Dict[str, int]:
>       result = {}  #type: Dict[str, int]
>       for line in input:
>           for word in line.split():
>               result[word] = result.get(word, 0) + 1
>       return result
>


I couldn't agree more with Dennis Brakhane saying:

> "I would be very sad to see annotations being limited to convey type
> information."

It'd be very tedious for us corporate web-service guys having to specify
things twice to get the advantages of Python's static typing -- whatever
they might end up being.

But it's not just that -- str[120] is a kind of string (of length 120)
the same way list[str] is a kind of list (that only contains strings).
But this gets out of hand fast: Why not say str[6,32,'[A-Fa-f0-9]+'] is
a hex string between lengths 6 and 32?

So my proposition is to have __getitem__ accept **kwargs. That way the
above becomes:

HexString = str[min_length=6, max_length=32, pattern='[A-Fa-f0-9]+']

This would elegantly blend in with duck typing as well.
object[write=callable] is a file-like object. Or maybe I should say
object[write=callable[bytes]] ?

Alternatively, having what I call "specializers" in typing.py could work
just as well:

HexString = str[min_length(6), max_length(32), pattern('[A-Fa-f0-9]+')]
IntToStrMapping = dict[key(int), value(str)]

But it looks hideous and as a bonus, invalid specializations like 
dict[pattern('[a-z0-9]+')]  will have to be dealt with.

Or, it's also possible to do what we're doing with Spyne and leave
builtins like str, list etc. alone and introduce a separate set of
markers to be used exclusively for type annotation purposes:

HexString = Unicode(min_length=6, max_length=32, pattern='[A-Fa-f0-9]+')
IntToStrMapping = Dict(key=int, value=str)

This has the disadvantage of doing generics with parens, which turned
out to be pretty confusing for some new users of Spyne.

While we're into the run-time checked types territory, why make do with
declarative restrictions and not pass arbitrary callables to validate
incoming objects? e.g.

PngData = bytes[validator=ensure_png]

where ensure_png is a callable[bytes] that ensures that the incoming
bytes object is indeed a valid png data.

Now you might think this proposal an abomination in the context of a
community of "consenting adults". But when you let your functions be
called by untrusted sources (which is, I'll argue, pretty much anyone
whose code listens to a socket one way or the other) precisely
expressing and applying such constraints is very very important.

IMO, how this kind of information is passed around needs to be
standardized for the same reasons Python got a standard event loop. We
already have a high amount of largely incompatible systems of varying
levels of sophistication and purposes for enforcing constraints to
incoming data.

I hope all this makes sense.

Best regards,
Burak

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140820/db3c1f19/attachment.html>


More information about the Python-ideas mailing list