[Python-Dev] inconsistency in annotated assigned targets

Joe Jevnik jjevnik at quantopian.com
Thu Jan 25 18:00:56 EST 2018


Currently there are many ways to introduce variables in Python; however,
only a few allow annotations. I was working on a toy language and chose to
base my syntax on Python's when I noticed that I could not annotate a loop
iteration variable. For example:

for x: int in range(5):
    ...

This led me to search for other places where new variables are introduced
and I noticed that the `as` target of a context manager cannot have an
annotation. In the case of a context manager, it would probably need
parenthesis to avoid ambiguity with a single-line with statement, for
example:

with ctx as (variable: annotation): body

Finally, you cannot annotate individual members of a destructuring
assignment like:

a: int, b: int, c: int = 1, 2, 3

Looking at the grammar, these appear to be `expr` or `exprlist` targets.
One change may be to allow arbitrary expressions to have an annotation .
This would be a small change to the grammar but would potentially have a
large effect on the language or static analysis tools.

I am posting on the mailing list to see if this is a real problem, and if
so, is it worth investing any time to address it. I would be happy to
attempt to fix this, but I don't want to start if people don't want the
change. Also, I apologize if this should have gone to python-idea; this
feels somewhere between a bug report and implementation question more than
a new feature so I wasn't sure which list would be more appropriate.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20180125/2fb76261/attachment.html>


More information about the Python-Dev mailing list