Skip to content

gh-132661: Disallow Template/str concatenation after PEP 750 spec update #135996

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 36 commits into from
Jul 21, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
5d5e187
Disallow explicit concat between Template and str
davepeck Jun 26, 2025
1cc778f
First pass at removing implicit Template/str concat
davepeck Jun 26, 2025
7ea498c
Merge branch 'main' into pep750-concat-update
davepeck Jun 30, 2025
d08c8b8
Merge branch 'main' into pep750-concat-update
davepeck Jul 8, 2025
c662041
Remove extraneous newline
davepeck Jul 8, 2025
b17b7c9
Add expected exception messages to tests
davepeck Jul 8, 2025
a0c1bb6
Remove _ast_unparse t/f implicit concat helper code
davepeck Jul 8, 2025
23988e8
Merge branch 'main' into pep750-concat-update
davepeck Jul 8, 2025
d7eadec
Add a parallel comment
davepeck Jul 8, 2025
cb1ad63
Add news blurb for PR.
davepeck Jul 8, 2025
741eaf2
One day, I may develop RST muscle memory.
davepeck Jul 8, 2025
8aeb512
Merge branch 'main' into pep750-concat-update
davepeck Jul 9, 2025
bf5447b
Merge branch 'main' into pep750-concat-update
davepeck Jul 9, 2025
7e7ea5a
Merge branch 'main' into pep750-concat-update
davepeck Jul 9, 2025
2f79337
Two minor tweaks
davepeck Jul 9, 2025
dacafdb
Use subtests; test both directions.
davepeck Jul 9, 2025
b272287
Update Objects/templateobject.c
davepeck Jul 9, 2025
ffae8e9
Update Misc/NEWS.d/next/Core_and_Builtins/2025-07-08-23-22-08.gh-issu…
davepeck Jul 9, 2025
c6ca45a
Fix bug in suggested change
davepeck Jul 9, 2025
091a2a9
Merge branch 'main' into pep750-concat-update
davepeck Jul 9, 2025
860caea
First pass at update to grammar and action_helpers.c
davepeck Jul 10, 2025
6a6f734
Merge branch 'main' into pep750-concat-update
davepeck Jul 10, 2025
860f81d
Simplify grammar
davepeck Jul 10, 2025
e0fe608
Use KNOWN_RANGE for string/t-string mix error messages
davepeck Jul 10, 2025
c9ed06b
Merge branch 'pep750-concat-update' of github.com:t-strings/cpython i…
davepeck Jul 10, 2025
3ebdd97
Merge branch 'main' into pep750-concat-update
davepeck Jul 10, 2025
ef1cd5c
Clean up ast_unparse.c append_tstring()
davepeck Jul 10, 2025
055af82
Merge branch 'main' into pep750-concat-update
davepeck Jul 15, 2025
ab393f7
Update Misc/NEWS.d/next/Core_and_Builtins/2025-07-08-23-22-08.gh-issu…
davepeck Jul 15, 2025
16e6a1f
Update Objects/templateobject.c
davepeck Jul 15, 2025
b81a807
Update error messages; add an assert(0)
davepeck Jul 15, 2025
b9bac0e
Simplify ast_unparase.c further
davepeck Jul 15, 2025
6b6acda
Fix news dangling reference (for now).
davepeck Jul 15, 2025
e5b03e8
Merge branch 'pep750-concat-update' of github.com:t-strings/cpython i…
davepeck Jul 15, 2025
6ea3af3
Fix exception assert message checks
davepeck Jul 16, 2025
5037f89
Remove no-longer used path in codegen.c
davepeck Jul 17, 2025
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
Prev Previous commit
Next Next commit
First pass at update to grammar and action_helpers.c
Squashed commit of the following:

commit 0941a3088578c1a8002e58ea6e11527bedf0e81e
Author: Dave <davepeck@gmail.com>
Date:   Thu Jul 10 01:49:24 2025 +0000

    Maybe cleaner?

commit e5a69c851a0f43a4e84325051a37de20282dd810
Author: Dave <davepeck@gmail.com>
Date:   Wed Jul 9 23:17:58 2025 +0000

    In progress: update grammar
  • Loading branch information
davepeck committed Jul 10, 2025
commit 860caea455b4a5876b13ed30896547244a697512
11 changes: 10 additions & 1 deletion Grammar/python.gram
Original file line number Diff line number Diff line change
Expand Up @@ -987,7 +987,10 @@ tstring[expr_ty] (memo):
_PyPegen_template_str(p, a, (asdl_expr_seq*)b, c)) }

string[expr_ty]: s[Token*]=STRING { _PyPegen_constant_from_string(p, s) }
strings[expr_ty] (memo): a[asdl_expr_seq*]=(fstring|string|tstring)+ { _PyPegen_concatenate_strings(p, a, EXTRA) }
strings[expr_ty] (memo):
| invalid_string_tstring_concat
| a[asdl_expr_seq*]=(fstring|string)+ { _PyPegen_concatenate_strings(p, a, EXTRA) }
| a[asdl_expr_seq*]=tstring+ { _PyPegen_concatenate_tstrings(p, a, EXTRA) }

list[expr_ty]:
| '[' a=[star_named_expressions] ']' { _PyAST_List(a, Load, EXTRA) }
Expand Down Expand Up @@ -1553,6 +1556,12 @@ invalid_tstring_conversion_character:
| '!' &(':' | '}') { RAISE_SYNTAX_ERROR_ON_NEXT_TOKEN("t-string: missing conversion character") }
| '!' !NAME { RAISE_SYNTAX_ERROR_ON_NEXT_TOKEN("t-string: invalid conversion character") }

invalid_string_tstring_concat:
| (fstring|string)+ a=tstring+ {
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(PyPegen_first_item(a, expr_ty), "cannot mix t-strings with strings or f-strings") }
| tstring+ a=(fstring|string)+ {
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(PyPegen_first_item(a, expr_ty), "cannot mix t-strings with strings or f-strings") }

invalid_arithmetic:
| sum ('+'|'-'|'*'|'/'|'%'|'//'|'@') a='not' b=inversion { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "'not' after an operator must be parenthesized") }
invalid_factor:
Expand Down
16 changes: 4 additions & 12 deletions Lib/test/test_tstring.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,30 +240,22 @@ def test_literal_concatenation(self):
self.assertTStringEqual(t, ("Hello, ", ""), [(name, "name")])
self.assertEqual(fstring(t), "Hello, Python")

# Test disallowed mix of t-string and string
# Test disallowed mix of t-string and string/f-string (incl. bytes)
what = 't'
expected_msg = 'cannot mix str and Template literals'
expected_msg = 'cannot mix t-strings with strings or f-strings'
for case in (
"t'{what}-string literal' 'str literal'",
"t'{what}-string literal' u'unicode literal'",
"t'{what}-string literal' f'f-string literal'",
"t'{what}-string literal' r'raw string literal'",
"t'{what}-string literal' rf'raw f-string literal'",
"t'{what}-string literal' b'bytes literal'",
"t'{what}-string literal' br'raw bytes literal'",
"'str literal' t'{what}-string literal'",
"u'unicode literal' t'{what}-string literal'",
"f'f-string literal' t'{what}-string literal'",
"r'raw string literal' t'{what}-string literal'",
"rf'raw f-string literal' t'{what}-string literal'",
):
with self.subTest(case):
with self.assertRaisesRegex(SyntaxError, expected_msg):
eval(case)

# Test disallowed mix of t-string and bytes
expected_msg = 'cannot mix bytes and nonbytes literals'
for case in (
"t'{what}-string literal' b'bytes literal'",
"t'{what}-string literal' br'raw bytes literal'",
"b'bytes literal' t'{what}-string literal'",
"br'raw bytes literal' t'{what}-string literal'",
):
Expand Down
23 changes: 4 additions & 19 deletions Parser/action_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -1834,8 +1834,8 @@ _build_concatenated_joined_str(Parser *p, asdl_expr_seq *strings,
return _PyAST_JoinedStr(values, lineno, col_offset, end_lineno, end_col_offset, p->arena);
}

static expr_ty
_build_concatenated_template_str(Parser *p, asdl_expr_seq *strings,
expr_ty
_PyPegen_concatenate_tstrings(Parser *p, asdl_expr_seq *strings,
int lineno, int col_offset, int end_lineno,
int end_col_offset, PyArena *arena)
{
Expand All @@ -1853,7 +1853,6 @@ _PyPegen_concatenate_strings(Parser *p, asdl_expr_seq *strings,
Py_ssize_t len = asdl_seq_LEN(strings);
assert(len > 0);

int t_string_found = 0;
int f_string_found = 0;
int unicode_string_found = 0;
int bytes_found = 0;
Expand All @@ -1872,29 +1871,20 @@ _PyPegen_concatenate_strings(Parser *p, asdl_expr_seq *strings,
case JoinedStr_kind:
f_string_found = 1;
break;
case TemplateStr_kind:
t_string_found = 1;
break;
default:
f_string_found = 1;
break;
}
}

// Cannot mix unicode and bytes
if ((unicode_string_found || f_string_found || t_string_found) && bytes_found) {
if ((unicode_string_found || f_string_found) && bytes_found) {
RAISE_SYNTAX_ERROR("cannot mix bytes and nonbytes literals");
return NULL;
}

// Cannot mix strings/f-strings and t-strings
if ((unicode_string_found || f_string_found) && t_string_found) {
RAISE_SYNTAX_ERROR("cannot mix str and Template literals");
return NULL;
}

// If it's only bytes or only unicode string, do a simple concat
if (!f_string_found && !t_string_found) {
if (!f_string_found) {
if (len == 1) {
return asdl_seq_GET(strings, 0);
}
Expand All @@ -1908,11 +1898,6 @@ _PyPegen_concatenate_strings(Parser *p, asdl_expr_seq *strings,
}
}

if (t_string_found) {
return _build_concatenated_template_str(p, strings, lineno,
col_offset, end_lineno, end_col_offset, arena);
}

return _build_concatenated_joined_str(p, strings, lineno,
col_offset, end_lineno, end_col_offset, arena);
}
Expand Down
Loading
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy