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

Barry Warsaw barry at python.org
Sat Aug 16 00:17:37 CEST 2014


On Aug 13, 2014, at 10:11 PM, Guido van Rossum wrote:

>We could refrain from using type annotations in the stdlib (similar to how
>we refrain from using Unicode identifiers). Mypy's stubs mechanism makes it
>possible to ship the type declarations for stdlib modules with mypy instead
>of baking them into the stdlib.

That would address my visceral reaction to the proposal.  I haven't had time
to read the entire thread, or supporting material, but my main concern is
about readability.  I don't think it's clear whether the impact of adding type
annotations is a pure win or loss.

OT1H maybe it helps because you would have to read less of the body of the
code to guess what the implicit types are for the arguments and return values
of a function.  I myself often have to "annotate" (i.e. add comments) around
code such as:

    # Map UUIDs to user object.
    users_by_uid = {}

Admittedly, it's often quite difficult when reading foreign code to understand
what are the types of the various constructs involved.  It can involve reading
more deeply into the call structure, or as I usually find, writing a bit of
exploratory code and then just stepping through with the debugger.  So type
annotations could be a win here.

OTOH there *is* a cost that could negatively impact readability.  It's more
text visually assaulting you. :) It can reduce the speed at which you can scan
code.  I'd say most of the time, you can pretty much deduce what's happening
fairly easily (with molasses-inducing outliers like above) so type annotations
may make things more confusing, especially if you have to mentally map between
annotation types and run time types that are similar but not quite the same.
If even more complex types are used, then you have to go digging into other
material (docs or code) to understand what the author meant by some of the
annotations.  Type annotations likely increase line length or vertical
real-estate, both of which can negatively impact readability.  And if
annotations are buggy, then it imposes even more cognitive dissonance.

In practice, who knows whether wholesale adoption of type annotation in the
stdlib *code* would be a net win or loss?  I don't think it's at all clear.

The other concern I'd have is whether annotating new stdlib code would become
mandatory, and whether there'd be a wholesale campaign to annotate existing
code.  Using stubs and not baking them into the stdlib alleviates those
concerns too.

My gut reaction is more negative than positive but I remain open minded until
I've had time to read more.

Cheers,
-Barry
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140815/8e2a877e/attachment-0001.sig>


More information about the Python-ideas mailing list