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

Antoine Pitrou antoine at python.org
Mon Aug 25 15:57:17 CEST 2014


Le 24/08/2014 23:12, Guido van Rossum a écrit :
 >
 > As I have said several times now, it fulfills exactly the original
 > (pre-PEP 3107) goal I had in mind for them.

But were you envisioning back then that said annotations would be
looked up without the regular execution environment?

PEP 3107 doesn't say anything about that (or rather, it says that 
annotations can be looked up on the function, but function objects only 
exist at run-time). Actually, the bytecode isn't very practical to work 
with to extract annotations, it seems:

 >>> def g():
...   def f(a: "foo") -> "bar": pass
...
 >>> dis.dis(g)
   2           0 LOAD_CONST               1 ('foo')
               3 LOAD_CONST               2 ('bar')
               6 LOAD_CONST               3 (('a', 'return'))
               9 LOAD_CONST               4 (<code object f at 
0x7f17339e2580, file "<stdin>", line 2>)
              12 LOAD_CONST               5 ('g.<locals>.f')
              15 EXTENDED_ARG             3
              18 MAKE_FUNCTION        196608
              21 STORE_FAST               0 (f)
              24 LOAD_CONST               0 (None)
              27 RETURN_VALUE

... so I suppose people would want to run an AST pass instead?
(and then evaluate the "annotations" parts of the AST by hand... uh)

 >     Your proposal seems determined by the fact that mypy has much
 >     grander ambitions (and therefore requires its insertion into regular
 >     Python), but it doesn't make use of that power at all, worse, it
 >     forbids others to use it.
 >
 > This remark about mypy's ambitions sounds delusional and paranoid.

It is entirely uncritical about mypy. It's fine to have other Python
(or Python-like) implementations, even with deliberately different
semantics. Such experimentations actually make the community much 
livelier than, say, PHP's or Ruby's.

I don't know at which point mypy changed goals (if it has), but there 
are still signs in the website of the goal of building a separate 
runtime (not necessarily a separate syntax), e.g.

"""Also some language features that are evaluated at runtime in Python
may happen during compilation in mypy when using the native semantics.
For example, mypy base classes may be bound during compilation (or
program loading, before evaluation), unlike Python."""

Also the fact that mypy supports constructs such as "List[int]()". This
is a set of design constraints that you're not bound to.

Regards

Antoine.




More information about the Python-ideas mailing list