[Python-ideas] ideas for type hints for variable: beyond comments

Ian ian.team.python at gmail.com
Tue Sep 1 12:24:15 CEST 2015

mypy currently inspects the comment on the line of first assignment for 
the variables to be type hinted.

It is logical that at some time python language will add support to 
allow these type hints to move from comments to the code as has happened 
for 'def' signatures.

One logical syntax would be to move from

i = 1           # Infer type int for i


i:int = 1       # no comment needed, but does not look attractive

The first question that arises is 'is the type inference legal for the 
additional uses. Having a 'second use' flagged by warning or error by 
either an external typechecker or even the language itself could pick up 
on accidental reuse of a name, but in practice accidentally creating a 
new variable through a typo can be more common. In python today the 
first use is the same as every other, so this change just does not feel 
comfortable. The other question is 'what about globals and nonlocals?'. 
Currently globals and nonlocals need a 'global' or 'nonlocal' statement 
to allow assignment, but what if these values are not assigned in scope? 
What if we allowed
global i:int


nonlocal i:int

and even

local i:int

Permitting a new keyword 'local' to me might bring far more symmetry 
between different cases.

It would also allow type hinting to be collected near the function 
definition and keep the type hinting clear of the main code.

Use of the 'local' keyword in the global namespace could indicate a 
value not accessible in other namespaces.

Personally I would like to go even further and allow some syntax to 
allow (or disable) flagging the use of new variables without type 
hinting as possible typos

I have a syntax in mind, but the idea is the discussion point, not the 
specific syntax.

Possibly what is here already is too much of a change of direction to 
consider for ideas already in progress?

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150901/de1de5dd/attachment.html>

More information about the Python-ideas mailing list