8000 bpo-42374: Allow unparenthesized walrus in genexps by lysnikolaou · Pull Request #23319 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content
8000

bpo-42374: Allow unparenthesized walrus in genexps #23319

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

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
bpo-42374: Allow unparenthesized walrus in genexps
This fixes a regression that was introduced by the new parser.
  • Loading branch information
lysnikolaou committed Nov 16, 2020
commit 1f188e40b270229ad4c81243dddee5e11752c3c6
2 changes: 1 addition & 1 deletion Grammar/python.gram
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ group[expr_ty]:
| '(' a=(yield_expr | named_expression) ')' { a }
| invalid_group
genexp[expr_ty]:
| '(' a=expression ~ b=for_if_clauses ')' { _Py_GeneratorExp(a, b, EXTRA) }
| '(' a=named_expression ~ b=for_if_clauses ')' { _Py_GeneratorExp(a, b, EXTRA) }
| invalid_comprehension
set[expr_ty]: '{' a=expressions_list '}' { _Py_Set(a, EXTRA) }
setcomp[expr_ty]:
Expand Down
10 changes: 10 additions & 0 deletions Lib/test/test_named_expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,16 @@ def g():
self.assertEqual(nonlocal_var, None)
f()

def test_named_expression_scope_in_genexp(self):
a = 1
b = [1, 2, 3, 4]
genexp = (c := i + a for i in b)

with self.assertRaises(NameError):
print(c) # c is not bound here
for idx, elem in enumerate(genexp):
self.assertEqual(elem, b[idx] + a)


if __name__ == "__main__":
unittest.main()
12 changes: 6 additions & 6 deletions Parser/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -11357,7 +11357,7 @@ group_rule(Parser *p)
return _res;
}

// genexp: '(' expression ~ for_if_clauses ')' | invalid_comprehension
// genexp: '(' named_expression ~ for_if_clauses ')' | invalid_comprehension
static expr_ty
genexp_rule(Parser *p)
{
Expand All @@ -11377,12 +11377,12 @@ genexp_rule(Parser *p)
UNUSED(_start_lineno); // Only used by EXTRA macro
int _start_col_offset = p->tokens[_mark]->col_offset;
UNUSED(_start_col_offset); // Only used by EXTRA macro
{ // '(' expression ~ for_if_clauses ')'
{ // '(' named_expression ~ for_if_clauses ')'
if (p->error_indicator) {
D(p->level--);
return NULL;
}
D(fprintf(stderr, "%*c> genexp[%d-%d]: %s\n", p->level, ' ', _mark, p->mark, "'(' expression ~ for_if_clauses ')'"));
D(fprintf(stderr, "%*c> genexp[%d-%d]: %s\n", p->level, ' ', _mark, p->mark, "'(' named_expression ~ for_if_clauses ')'"));
int _cut_var = 0;
Token * _literal;
Token * _literal_1;
Expand All @@ -11391,7 +11391,7 @@ genexp_rule(Parser *p)
if (
(_literal = _PyPegen_expect_token(p, 7)) // token='('
&&
(a = expression_rule(p)) // expression
(a = named_expression_rule(p)) // named_expression
&&
(_cut_var = 1)
&&
Expand All @@ -11400,7 +11400,7 @@ genexp_rule(Parser *p)
(_literal_1 = _PyPegen_expect_token(p, 8)) // token=')'
)
{
D(fprintf(stderr, "%*c+ genexp[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "'(' expression ~ for_if_clauses ')'"));
D(fprintf(stderr, "%*c+ genexp[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "'(' named_expression ~ for_if_clauses ')'"));
Token *_token = _PyPegen_get_last_nonnwhitespace_token(p);
if (_token == NULL) {
D(p->level--);
Expand All @@ -11420,7 +11420,7 @@ genexp_rule(Parser *p)
}
p->mark = _mark;
D(fprintf(stderr, "%*c%s genexp[%d-%d]: %s failed!\n", p->level, ' ',
p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "'(' expression ~ for_if_clauses ')'"));
p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "'(' named_expression ~ for_if_clauses ')'"));
if (_cut_var) {
D(p->level--);
return NULL;
Expand Down
0