Skip to content

Commit

Permalink
bpo-42316: Allow unparenthesized walrus operator in indexes (GH-23317)
Browse files Browse the repository at this point in the history
  • Loading branch information
lysnikolaou authored Nov 16, 2020
1 parent cb3e5ed commit cae6018
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Grammar/python.gram
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ slices[expr_ty]:
| a[asdl_expr_seq*]=','.slice+ [','] { _Py_Tuple(a, Load, EXTRA) }
slice[expr_ty]:
| a=[expression] ':' b=[expression] c=[':' d=[expression] { d }] { _Py_Slice(a, b, c, EXTRA) }
| a=expression { a }
| a=named_expression { a }
atom[expr_ty]:
| NAME
| 'True' { _Py_Constant(Py_True, NULL, EXTRA) }
Expand Down
21 changes: 21 additions & 0 deletions Lib/test/test_named_expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,27 @@ def test_named_expression_assignment_16(self):
fib = {(c := a): (a := b) + (b := a + c) - b for __ in range(6)}
self.assertEqual(fib, {1: 2, 2: 3, 3: 5, 5: 8, 8: 13, 13: 21})

def test_named_expression_assignment_17(self):
a = [1]
element = a[b:=0]
self.assertEqual(b, 0)
self.assertEqual(element, a[0])

def test_named_expression_assignment_18(self):
class TwoDimensionalList:
def __init__(self, two_dimensional_list):
self.two_dimensional_list = two_dimensional_list

def __getitem__(self, index):
return self.two_dimensional_list[index[0]][index[1]]

a = TwoDimensionalList([[1], [2]])
element = a[b:=0, c:=0]
self.assertEqual(b, 0)
self.assertEqual(c, 0)
self.assertEqual(element, a.two_dimensional_list[b][c])



class NamedExpressionScopeTest(unittest.TestCase):

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Allow an unparenthesized walrus in subscript indexes.
12 changes: 6 additions & 6 deletions Parser/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -10639,7 +10639,7 @@ slices_rule(Parser *p)
return _res;
}

// slice: expression? ':' expression? [':' expression?] | expression
// slice: expression? ':' expression? [':' expression?] | named_expression
static expr_ty
slice_rule(Parser *p)
{
Expand Down Expand Up @@ -10701,18 +10701,18 @@ slice_rule(Parser *p)
D(fprintf(stderr, "%*c%s slice[%d-%d]: %s failed!\n", p->level, ' ',
p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "expression? ':' expression? [':' expression?]"));
}
{ // expression
{ // named_expression
if (p->error_indicator) {
D(p->level--);
return NULL;
}
D(fprintf(stderr, "%*c> slice[%d-%d]: %s\n", p->level, ' ', _mark, p->mark, "expression"));
D(fprintf(stderr, "%*c> slice[%d-%d]: %s\n", p->level, ' ', _mark, p->mark, "named_expression"));
expr_ty a;
if (
(a = expression_rule(p)) // expression
(a = named_expression_rule(p)) // named_expression
)
{
D(fprintf(stderr, "%*c+ slice[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "expression"));
D(fprintf(stderr, "%*c+ slice[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "named_expression"));
_res = a;
if (_res == NULL && PyErr_Occurred()) {
p->error_indicator = 1;
Expand All @@ -10723,7 +10723,7 @@ slice_rule(Parser *p)
}
p->mark = _mark;
D(fprintf(stderr, "%*c%s slice[%d-%d]: %s failed!\n", p->level, ' ',
p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "expression"));
p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "named_expression"));
}
_res = NULL;
done:
Expand Down

0 comments on commit cae6018

Please sign in to comment.