[Python-ideas] Proposal to extend PEP 484 (gradual typing) to support Python 2.7

Terry Reedy tjreedy at udel.edu
Mon Jan 11 20:44:45 EST 2016


On 1/11/2016 5:38 PM, M.-A. Lemburg wrote:

> To clarify: My suggestion to use a simple decorator with essentially
> the same syntax as proposed for the "# type: comments " was meant
> as *additional* allowed syntax, not necessarily as the only one
> to standardize.

Code with type comments will run on any standard 2.7 interpreter.  Code 
with an @typehint decorator will have to either run on a nonstandard 
interpreter or import 'typehint' from somewhere other than the stdlib or 
define 'typehint' at the top of the file or have the decorators stripped 
out before public distribution.  To me, these options come close to 
making the decorator inappropriate as a core dev recommendation.

However, the new section of the PEP could have a short paragraph that 
mentions @typehint(typestring) as a possible alternative (with the 
options given above) and recommend that if a decorator is used, then the 
name should be 'typehint' (or something else agreed on) and that the 
typestring should be a quoted version of what would follow '# type: ' in 
a comment, 'as already defined above' (in the previous recommendation).

In other words, Guido's current addition has two recommendations:
1. the syntax for a typestring
2. the use of a typestring (append it to a '# type: ' comment)
If a decorator alternative uses the same syntax, a checker would need 
just one typestring parser.  I think the conditional recommendation 
would be within the scope of what is appropriate for us to do.

> I'm a bit worried that by standardizing on using comments
> for these annotations only, we'll end up having people not
> use the type annotations because they simply don't like the
> style of having function bodies begin with comments instead
> of doc-strings. I certainly wouldn't want to clutter up my
> code like that. Tools parsing Python 2 source code may
> also have a problem with this (e.g. not recognize the
> doc-string anymore).

I have to admit that I was not fully cognizant before than a comment 
could precede a docstring.

-- 
Terry Jan Reedy



More information about the Python-ideas mailing list