https://github.com/python/cpython/commit/db2d8b6db1b56c2bd3802b86f9b76da33e8898d7
commit: db2d8b6db1b56c2bd3802b86f9b76da33e8898d7
branch: main
author: Pablo Galindo Salgado <[email protected]>
committer: pablogsal <[email protected]>
date: 2024-07-26T16:29:41Z
summary:

gh-122300: Preserve AST nodes for format specifiers with single elements 
(#122308)

files:
A Misc/NEWS.d/next/Core and 
Builtins/2024-07-26-14-05-51.gh-issue-122300.SVIF-l.rst
M Doc/library/ast.rst
M Lib/test/test_ast.py
M Parser/action_helpers.c

diff --git a/Doc/library/ast.rst b/Doc/library/ast.rst
index d05ad1e2a7854f..dd5dd5ca4e9e32 100644
--- a/Doc/library/ast.rst
+++ b/Doc/library/ast.rst
@@ -316,7 +316,9 @@ Literals
                             args=[
                                 Name(id='a', ctx=Load())]),
                         conversion=-1,
-                        format_spec=Constant(value='.3'))]))
+                        format_spec=JoinedStr(
+                            values=[
+                                Constant(value='.3')]))]))
 
 
 .. class:: List(elts, ctx)
diff --git a/Lib/test/test_ast.py b/Lib/test/test_ast.py
index 5144187d7c3ddd..55725ec36fd3a7 100644
--- a/Lib/test/test_ast.py
+++ b/Lib/test/test_ast.py
@@ -3638,7 +3638,7 @@ def main():
 ('Expression', ('Subscript', (1, 0, 1, 10), ('List', (1, 0, 1, 3), 
[('Constant', (1, 1, 1, 2), 5, None)], ('Load',)), ('Slice', (1, 4, 1, 9), 
('Constant', (1, 4, 1, 5), 1, None), ('Constant', (1, 6, 1, 7), 1, None), 
('Constant', (1, 8, 1, 9), 1, None)), ('Load',))),
 ('Expression', ('IfExp', (1, 0, 1, 21), ('Name', (1, 9, 1, 10), 'x', 
('Load',)), ('Call', (1, 0, 1, 5), ('Name', (1, 0, 1, 3), 'foo', ('Load',)), 
[], []), ('Call', (1, 16, 1, 21), ('Name', (1, 16, 1, 19), 'bar', ('Load',)), 
[], []))),
 ('Expression', ('JoinedStr', (1, 0, 1, 6), [('FormattedValue', (1, 2, 1, 5), 
('Name', (1, 3, 1, 4), 'a', ('Load',)), -1, None)])),
-('Expression', ('JoinedStr', (1, 0, 1, 10), [('FormattedValue', (1, 2, 1, 9), 
('Name', (1, 3, 1, 4), 'a', ('Load',)), -1, ('Constant', (1, 5, 1, 8), '.2f', 
None))])),
+('Expression', ('JoinedStr', (1, 0, 1, 10), [('FormattedValue', (1, 2, 1, 9), 
('Name', (1, 3, 1, 4), 'a', ('Load',)), -1, ('JoinedStr', (1, 4, 1, 8), 
[('Constant', (1, 5, 1, 8), '.2f', None)]))])),
 ('Expression', ('JoinedStr', (1, 0, 1, 8), [('FormattedValue', (1, 2, 1, 7), 
('Name', (1, 3, 1, 4), 'a', ('Load',)), 114, None)])),
 ('Expression', ('JoinedStr', (1, 0, 1, 11), [('Constant', (1, 2, 1, 6), 
'foo(', None), ('FormattedValue', (1, 6, 1, 9), ('Name', (1, 7, 1, 8), 'a', 
('Load',)), -1, None), ('Constant', (1, 9, 1, 10), ')', None)])),
 ]
diff --git a/Misc/NEWS.d/next/Core and 
Builtins/2024-07-26-14-05-51.gh-issue-122300.SVIF-l.rst b/Misc/NEWS.d/next/Core 
and Builtins/2024-07-26-14-05-51.gh-issue-122300.SVIF-l.rst
new file mode 100644
index 00000000000000..6b58f89247d1d4
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and 
Builtins/2024-07-26-14-05-51.gh-issue-122300.SVIF-l.rst 
@@ -0,0 +1,2 @@
+Preserve AST nodes for f-string with single-element format specifiers. Patch
+by Pablo Galindo
diff --git a/Parser/action_helpers.c b/Parser/action_helpers.c
index db6f872c7224d1..1972c606827cdb 100644
--- a/Parser/action_helpers.c
+++ b/Parser/action_helpers.c
@@ -1010,7 +1010,8 @@ _PyPegen_setup_full_format_spec(Parser *p, Token *colon, 
asdl_expr_seq *spec, in
         spec = resized_spec;
     }
     expr_ty res;
-    if (asdl_seq_LEN(spec) == 0) {
+    Py_ssize_t n = asdl_seq_LEN(spec);
+    if (n == 0 || (n == 1 && asdl_seq_GET(spec, 0)->kind == Constant_kind)) {
         res = _PyAST_JoinedStr(spec, lineno, col_offset, end_lineno,
                                     end_col_offset, p->arena);
     } else {

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to