-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
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
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!
Happy to help out / review this PR if you wanna take a stab at it. Feel free to reach out if you have any specific questions. |
Today was, alas, not that day.
@lysnikolaou Think this is ready. The only question for me is whether we want to go further with |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you! Several small nitpicks :)
Misc/NEWS.d/next/Core_and_Builtins/2025-07-08-23-22-08.gh-issue-132661.34ftJl.rst
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @davepeck for working on this! Looks great in general. Left a few unimportant comments and one more significant one about how to implement this in the parser. Also, we'll need to change ast_unparse.c
around https://github.com/python/cpython/blob/main/Python/ast_unparse.c#L715.
Parser/action_helpers.c
Outdated
// 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; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not the way to do this. Instead, we should be doing this at the parser level. Right now we're doing this:
atom[expr_ty]:
| NAME
| 'True' { _PyAST_Constant(Py_True, NULL, EXTRA) }
| 'False' { _PyAST_Constant(Py_False, NULL, EXTRA) }
| 'None' { _PyAST_Constant(Py_None, NULL, EXTRA) }
| &(STRING|FSTRING_START|TSTRING_START) strings # This'll change
| NUMBER
| &'(' (tuple | group | genexp)
| &'[' (list | listcomp)
| &'{' (dict | set | dictcomp | setcomp)
| '...' { _PyAST_Constant(Py_Ellipsis, NULL, EXTRA) }
strings[expr_ty] (memo): a[asdl_expr_seq*]=(fstring|string|tstring)+ { _PyPegen_concatenate_strings(p, a, EXTRA) }
Instead, we should be doing something like:
atom[expr_ty]:
| NAME
| 'True' { _PyAST_Constant(Py_True, NULL, EXTRA) }
| 'False' { _PyAST_Constant(Py_False, NULL, EXTRA) }
| 'None' { _PyAST_Constant(Py_None, NULL, EXTRA) }
| &(STRING|FSTRING_START) strings # We now have two alternatives
| &TSTRING_START tstrings
| NUMBER
| &'(' (tuple | group | genexp)
| &'[' (list | listcomp)
| &'{' (dict | set | dictcomp | setcomp)
| '...' { _PyAST_Constant(Py_Ellipsis, NULL, EXTRA) }
tstrings[expr_ty] (memo): a[asdl_expr_seq*]=tstring+ { <new action here> }
strings[expr_ty] (memo): a[asdl_expr_seq*]=(fstring|string)+ { _PyPegen_concatenate_strings(p, a, EXTRA) }
Notice how instead of intermixing the three rules in one sequence, we can now only mix string
s and fstring
s, but tstring
s can only be concatenated with other tstring
s. The new action should probably be _build_concatenated_template_str
renamed with a _PyPegen
prefix, but I'm not sure whether that'll work unchanged, it might need some editing. Also, some of the invalid_*
rules might need changing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure thing, will do.
I just wasn't sure we wanted the extra complexity in the grammar.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lysnikolaou Would you be okay with something more along the lines of:
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) }
...
invalid_string_tstring_concat:
| (fstring|string)+ a[expr_ty]=tstring { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "cannot mix t-strings with strings or f-strings") }
| tstring+ a[expr_ty]=(fstring|string) { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "cannot mix t-strings with strings or f-strings") }
or do we prefer separating strings
from tstrings
(and I guess having two different invalid
rules)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, this looks even better. Can we use RAISE_SYNTAX_ERROR_KNOWN_RANGE
instead of KNOWN_LOCATION
so that the error is a bit clearer?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, is this ready for another review or should I wait for a ping from you?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we use RAISE_SYNTAX_ERROR_KNOWN_RANGE
Ah, yeah, that'd be nicer; will do. I guess the "ideal" range would include exactly two neighboring literals that conflict.
Also, is this ready for another review or should I wait for a ping from you?
Not yet, I'll ping you. Thanks! (Still have more to tease apart in action_helpers.c
and also need to tackle ast_unparse
.)
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
Co-authored-by: sobolevn <mail@sobolevn.me>
…e-132661.34ftJl.rst Co-authored-by: sobolevn <mail@sobolevn.me>
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
Okay, I think this is ready! Updated the grammar and actions; fixed up ast_unparse.c. Two questions:
(PS: dear Bedevere: |
Thanks for making the requested changes! @lysnikolaou, @JelleZijlstra: please review the changes made to this pull request. |
Following the steering council decision and corresponding update to PEP750, we are removing support for both implicit and explicit
Template
/str
concatenation.