[New-bugs-announce] [issue31263] Assigning to subscript/slice of literal is permitted

Isaac Elliott report at bugs.python.org
Wed Aug 23 01:02:36 EDT 2017

New submission from Isaac Elliott:

In Python 3.5 and 3.6 (at least), the language reference presents a grammar that disallows assignment to literals.

For example, `(a for 1 in [1,2,3])` is a syntax error, as is `(1, a) = (2, 3)`.

However the grammar doesn't prevent assignment to subscripted or sliced literals.

For example neither `(a for [1,2,3][0] in [1,2,3])` nor `([1,2,3][0], a) = (2, 3)` are considered syntax errors.

Similar behavior is exhibited for slices.

The problem is that the `target_list` production (https://docs.python.org/3.5/reference/simple_stmts.html#grammar-token-target_list) reuses the `subscription` and `slicing` productions which both use the `primary` production, allowing literals on their left side.

messages: 300740
nosy: Isaac Elliott
priority: normal
severity: normal
status: open
title: Assigning to subscript/slice of literal is permitted
type: behavior
versions: Python 3.5, Python 3.6

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list