Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-42316: Allow unparenthesized walrus operator in indexes #23317

Merged
merged 3 commits into from
Nov 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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])

lysnikolaou marked this conversation as resolved.
Show resolved Hide resolved
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.
lysnikolaou marked this conversation as resolved.
Show resolved Hide resolved
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