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

gh-106396: Special-case empty format spec to gen empty JoinedStr node #106401

Merged
merged 4 commits into from
Jul 4, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions Lib/test/test_fstring.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,24 @@ def test_ast_line_numbers_with_parentheses(self):
self.assertEqual(wat2.end_col_offset, 17)
self.assertEqual(fstring.end_col_offset, 18)

def test_ast_fstring_empty_format_spec(self):
expr = "f'{expr:}'"

mod = ast.parse(expr)
self.assertEqual(type(mod), ast.Module)
self.assertEqual(len(mod.body), 1)

fstring = mod.body[0].value
self.assertEqual(type(fstring), ast.JoinedStr)
self.assertEqual(len(fstring.values), 1)

fv = fstring.values[0]
self.assertEqual(type(fv), ast.FormattedValue)

format_spec = fv.format_spec
self.assertEqual(type(format_spec), ast.JoinedStr)
self.assertEqual(len(format_spec.values), 0)

def test_docstring(self):
def f():
f'''Not a docstring'''
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
When the format specification of an f-string expression is empty, the parser now
generates an empty :class:`ast.JoinedStr` node for it instead of an one-element
:class:`ast.JoinedStr` with an empty string :class:`ast.Constant`.
10 changes: 10 additions & 0 deletions Parser/action_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -997,6 +997,16 @@ _PyPegen_setup_full_format_spec(Parser *p, Token *colon, asdl_expr_seq *spec, in
if (!spec) {
return NULL;
}

if (asdl_seq_LEN(spec) == 1) {
lysnikolaou marked this conversation as resolved.
Show resolved Hide resolved
expr_ty e = asdl_seq_GET(spec, 0);
if (e->kind == Constant_kind
&& PyUnicode_Check(e->v.Constant.value)
&& PyUnicode_GetLength(e->v.Constant.value) == 0) {
spec = _Py_asdl_expr_seq_new(0, arena);
}
}

expr_ty res = _PyAST_JoinedStr(spec, lineno, col_offset, end_lineno, end_col_offset, p->arena);
if (!res) {
return NULL;
Expand Down