From 1c786ee6273377ac68a98483c1e6e7cd81bde332 Mon Sep 17 00:00:00 2001 From: Batuhan Taskaya Date: Tue, 11 Oct 2022 02:54:09 +0300 Subject: [PATCH 1/1] Add support for named exprs inside function calls as gen-exps (#3327) --- CHANGES.md | 4 ++++ src/blib2to3/Grammar.txt | 2 +- tests/data/py_310/pep_572_py310.py | 11 +++++++++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index 3a8cf4d..ba9f4c0 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -26,6 +26,10 @@ +- Parsing support has been added for walruses inside generator expression that are + passed as function args (for example, + `any(match := my_re.match(text) for text in texts)`) (#3327). + ### Performance diff --git a/src/blib2to3/Grammar.txt b/src/blib2to3/Grammar.txt index ac7ad76..bd8a452 100644 --- a/src/blib2to3/Grammar.txt +++ b/src/blib2to3/Grammar.txt @@ -186,7 +186,7 @@ arglist: argument (',' argument)* [','] # multiple (test comp_for) arguments are blocked; keyword unpackings # that precede iterable unpackings are blocked; etc. argument: ( test [comp_for] | - test ':=' test | + test ':=' test [comp_for] | test 'as' test | test '=' asexpr_test | '**' test | diff --git a/tests/data/py_310/pep_572_py310.py b/tests/data/py_310/pep_572_py310.py index 2aef589..cb82b2d 100644 --- a/tests/data/py_310/pep_572_py310.py +++ b/tests/data/py_310/pep_572_py310.py @@ -2,3 +2,14 @@ x[a:=0] x[a:=0, b:=1] x[5, b:=0] + +# Walruses are allowed inside generator expressions on function calls since 3.10. +if any(match := pattern_error.match(s) for s in buffer): + if match.group(2) == data_not_available: + # Error OK to ignore. + pass + +f(a := b + c for c in range(10)) +f((a := b + c for c in range(10)), x) +f(y=(a := b + c for c in range(10))) +f(x, (a := b + c for c in range(10)), y=z, **q) -- 2.39.5