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

Steven D'Aprano steve at pearwood.info
Thu Aug 14 20:30:14 CEST 2014


On Thu, Aug 14, 2014 at 10:46:43AM -0700, Gregory P. Smith wrote:

> My overarching concern with the entire proposal is that adding this would
> just be yet more syntax added to the language with not much use that
> doesn't go far enough.

This isn't new syntax. Functional annotations have been in Python since 
Python 3.0. What's new here is blessing one specific meaning for that 
syntax as the One Offical use for annotations.

I'd rather:

- bless function annotations for static type checking as the default
  meaning, but allowing code to opt-out and use annotations for
  something else;

- encourage the various type checkers and linters to come up with a
  standard syntax for type annotations.

I'm not convinced that mypy syntax is yet mature enough to be that 
standard. But, perhaps if the typing module is given provisional status, 
maybe it could be a good start.


> If you do add a module for this, at least consider hiding it behind a "from
> __future__ import some_module_full_of_annotation_related_things" instead of
> making it a new no-op top level module.

I'm not sure what this comment means. Did you read Guido's first post in 
this thread? I thought he was clear that to get type checking, you would 
do this:

from typing import Dict

def function(d: Dict[str, int])-> int:
    ...

I bet you can guess what that does, but in case you can't, it declares 
that argument d is a Dict with str keys and int values, and the return 
result is an int. I'm not sure where you get the idea of a no-op top 
level module from.

"from __future__ import ..." is inappropriate too, since that is 
intended for changes to compiler semantics (e.g. new syntax). This is 
existing syntax, and the actual type checking itself will be delegated 
to a separate product, mypy, which as Guido stated will behave as a 
linter. That means that the type checks won't do anything unless you 
have mypy installed, and you can still run your code under any Python 
compiler you like. As I understand it, CPython itself requires no 
changes to make this work, just the addition of typing.py from the mypy 
project and a policy change to the use of annotations.



-- 
Steven


More information about the Python-ideas mailing list