[Python-Dev] inconsistency in annotated assigned targets

Guido van Rossum guido at python.org
Thu Jan 25 22:14:39 EST 2018


PEP 526 has this in the "Rejected/Postponed Proposals" section:

- **Allow annotations in** ``with`` **and** ``for`` **statement:**
  This was rejected because in ``for`` it would make it hard to spot the
actual
  iterable, and in ``with`` it would confuse the CPython's LL(1) parser.


On Thu, Jan 25, 2018 at 3:17 PM, Jelle Zijlstra <jelle.zijlstra at gmail.com>
wrote:

>
>
> 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
>> 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.
>>
> 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
>> https://mail.python.org/mailman/listinfo/python-dev
>> Unsubscribe: https://mail.python.org/mailman/options/python-dev/jelle.
>> zijlstra%40gmail.com
>>
>>
>
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> https://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe: https://mail.python.org/mailman/options/python-dev/
> guido%40python.org
>
>


-- 
--Guido van Rossum (python.org/~guido)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20180125/aa5331ec/attachment.html>


More information about the Python-Dev mailing list