[Python-ideas] Structural type checking for PEP 484

Sven R. Kunze srkunze at mail.de
Wed Sep 16 22:57:29 CEST 2015


On 11.09.2015 00:22, Andrew Barnert wrote:
> On Sep 10, 2015, at 09:42, Sven R. Kunze <srkunze at mail.de> wrote:
>> I mean when I am really going to touch that file to improve documentation (which annotations are a piece of), I am going to add more information for the reader of my API and that mostly will be describing the behavior of the API.
> As a bit of useless anecdotal evidence:
>
> After starting to play with MyPy when Guido first announced the idea, I haven't actually started using static type checking seriously, but I have started writing annotations for some of my functions. It feels like a concise and natural way to say "this function wants two integers", and it reads as well as it writes. Of course there's no reason I couldn't have been doing this since 3.0, but I wasn't, and now I am.
>
> Try playing around with it and see if you get the same feeling. Since everyone is thinking about the random module right now, and it makes a great example of what I'm talking about, specify which functions take/return int vs. float, which need a real int vs. anything Integral, etc., and how much more easily you absorb the information than if it's in the middle of a sentence in the docstring.
>
> Anyway, I don't actually annotate every function (or every function except the ones that are so simple that any checker or reader that couldn't infer the types is useless, the way I would in Haskell), just the ones where the types seem like an important part of the semantics. So I haven't missed the more complex features the way I expected to. But I've still got no problem with them being added as we go along, of course. :)

Thanks for the anecdote. It's good to hear you don't do it for every 
function and I am glad it helps you a lot. :)

Do you know what makes me sad? If you do that for this function but 
don't do it for another what is the guideline then? Python Zen tells us 
to have one obvious way to do sth. At least for me, it's not obvious 
anymore when to annotate and when not to annote. Just a random guess 
depending on the moon phase? :(

Sometimes and sometimes that. That can't be right for something to basic 
like types. Couldn't these problems not be solved by further research on 
typecheckers?

Btw. I can tell the same anecdote when switching from C/C++/C#/Java to 
Python. It was like a liberation---no explicit type declarations 
anymore. I was baffled and frightened the first week using it. But I 
love it now and I don't want to give that freedom up. Maybe, that's why 
I am reluctant to use it in production.

But as said, I like the theoretical discussion around it. :)

Best,
Sven


More information about the Python-ideas mailing list