[Python-Dev] inconsistency in annotated assigned targets
2018-01-25 15:00 GMT-08:00 Joe Jevnik via Python-Dev <python-dev at python.org>
> 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
> 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.
I have written a fair amount of code with variable annotations, and I don't
remember ever wanting to add annotations in any of the three contexts you
mention. In practice, variable annotations are usually needed for
class/instance variables and for variables whose type the type checker
can't infer. The types of loop iteration variables and context manager
assignment targets can almost always be inferred trivially.
> Python-Dev mailing list
> Python-Dev at python.org
> Unsubscribe: https://mail.python.org/mailman/options/python-dev/
-------------- next part --------------
An HTML attachment was scrubbed...