Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Next Next commit
gh-133196: Improve error message for ft"" case
  • Loading branch information
sobolevn committed Apr 30, 2025
commit 4f57f3df6fc63f24e3099dfbfc85e07259a6123a
9 changes: 9 additions & 0 deletions Lib/test/test_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -1877,6 +1877,15 @@
Traceback (most recent call last):
SyntaxError: cannot assign to f-string expression here. Maybe you meant '==' instead of '='?

>>> ft'abc'
Traceback (most recent call last):
SyntaxError: can't use 'f' and 't' string prefixes at the same time

>>> x = 1
Comment thread
sobolevn marked this conversation as resolved.
Outdated
>>> tf"{x=}"
Traceback (most recent call last):
SyntaxError: can't use 'f' and 't' string prefixes at the same time

>>> t'{x}' = 42
Traceback (most recent call last):
SyntaxError: cannot assign to t-string expression here. Maybe you meant '==' instead of '='?
Expand Down
9 changes: 7 additions & 2 deletions Parser/lexer/lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -660,17 +660,22 @@ tok_get_normal_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct t
else if (!(saw_r || saw_u) && (c == 'r' || c == 'R')) {
saw_r = 1;
}
else if (!(saw_f || saw_b || saw_u || saw_t) && (c == 'f' || c == 'F')) {
else if (!(saw_f || saw_b || saw_u) && (c == 'f' || c == 'F')) {
saw_f = 1;
}
else if (!(saw_t || saw_b || saw_u || saw_f) && (c == 't' || c == 'T')) {
else if (!(saw_t || saw_b || saw_u) && (c == 't' || c == 'T')) {
saw_t = 1;
}
else {
break;
}
c = tok_nextc(tok);
if (c == '"' || c == '\'') {
if (saw_f && saw_t) {
return MAKE_TOKEN(_PyTokenizer_syntaxerror(
tok,
"can't use 'f' and 't' string prefixes at the same time"));
}
if (saw_f || saw_t) {
goto f_string_quote;
}
Expand Down
Loading