Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fa19a25

Browse files
isidenticalpablogsal
authored andcommitted
Add support for PEP572 in ast_unparse.c (GH-13337)
1 parent eab9965 commit fa19a25

3 files changed

Lines changed: 16 additions & 0 deletions

File tree

Lib/test/test_future.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,8 @@ def test_annotations(self):
275275
eq('f((x for x in a), 2)')
276276
eq('(((a)))', 'a')
277277
eq('(((a, b)))', '(a, b)')
278+
eq("(x:=10)")
279+
eq("f'{(x:=10):=10}'")
278280

279281

280282
if __name__ == "__main__":
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add NamedExpression kind support to ast_unparse.c

Python/ast_unparse.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,17 @@ append_ast_await(_PyUnicodeWriter *writer, expr_ty e, int level)
809809
return 0;
810810
}
811811

812+
static int
813+
append_named_expr(_PyUnicodeWriter *writer, expr_ty e, int level)
814+
{
815+
APPEND_STR_IF(level > PR_TUPLE, "(");
816+
APPEND_EXPR(e->v.NamedExpr.target, PR_ATOM);
817+
APPEND_STR(":=");
818+
APPEND_EXPR(e->v.NamedExpr.value, PR_ATOM);
819+
APPEND_STR_IF(level > PR_TUPLE, ")");
820+
return 0;
821+
}
822+
812823
static int
813824
append_ast_expr(_PyUnicodeWriter *writer, expr_ty e, int level)
814825
{
@@ -867,6 +878,8 @@ append_ast_expr(_PyUnicodeWriter *writer, expr_ty e, int level)
867878
return append_ast_list(writer, e);
868879
case Tuple_kind:
869880
return append_ast_tuple(writer, e, level);
881+
case NamedExpr_kind:
882+
return append_named_expr(writer, e, level);
870883
default:
871884
PyErr_SetString(PyExc_SystemError,
872885
"unknown expression kind");

0 commit comments

Comments
 (0)