You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Bug with the colorizer dependent on tokenizer #140367
It is a SyntaxError. However, in many web page, the 123456 are colored as string.
This question appeared also in REPL. The IDLE had no this question originally, but after using tokenizer to color the codes, it appears.
It may mislead the learners.
Now that the github page has no question on it, but discuss has.
This is about the REPL; IDLE works according to OP's expectations.
In either case, the code has to be pasted in to demonstrate the problem — otherwise, the interpreter will report a syntax error as soon as the Enter key is pressed after the opening quotation mark.
However, in IDLE the invalid pasted code will show up with invalid highlighting: the intended contents of the string literal have the default colour rather than string-literal colour. (The opening quote will, however, only be highlighted after pressing Enter.)
At the REPL, meanwhile, the pasted code is coloured as if it were a valid string literal.
I'm not sure that this is particularly important, since proper feedback is almost immediate and even delaying that feedback a little bit requires copying and pasting invalid code from elsewhere. However, it's a strange inconsistency.
As for syntax highlighting on web pages, that is obviously outside of CPython's control. Every web page author can make the code appear with whatever styling is desired, even if it's misleading, and there's nothing that CPython could do about that. (As far as I'm aware, the CPython documentation doesn't show invalid string literals like this, except specifically to explain that they are invalid.)
While there may be a preference for how syntax erroneous code is colorized, there is no correct way. I believe @ambv has said something similar. There is no violation of even an implied specification. Hence, I do not see a bug here and am removing the bug label.
That said, I cannot reproduce the claimed REPL behavior. After copying the code above and running with the same binary, each line is run separately, as with the old REPL.
In fact, there is another problem in it: the syntax label is unstable in this absolute invalid syntax.
For example, I wrote the code, but I want to print a string before to debug it. After I input a " or ' the code after were change its color. This effect is bad.
That said, I cannot reproduce the claimed REPL behavior. After copying the code above and running with the same binary, each line is run separately, as with the old REPL.
On Linux, it is not run till one presses Enter, so I can reproduce the reported colorizing.
It's not just for strings that we have this disparity. For instance, on Linux:
wrong=fromximport (
1+1
)
In the REPL, I get (before hitting ENTER):
and once I hit ENTER, I will have a SyntaxError. On the other hand, in IDLE (before hitting ENTER), I get:
When someone C/C in the REPL, it gets colorized independently of whether it's legit code or not, while in IDLE, it gets colorized but not everything. I don't know if it's possible to have a common implementation for colorizing inputs by REPL/IDLE before hitting ENTER (I would say yes since they should only depend on the input lines that could be tokenizied in one go and then colorized).
In some situations, it makes sense to keep highlights (for instance, in my example, if I were to change 1 + 1 to a legitimate import, but keep wrong =, with colorization, I would see that wrong = should be removed but without it, it would be less explicit).
Is there anything actionable here? This behavior is intentional: we are not parsing the input as it will be partial (i.e. incomplete, therefore "invalid") in most cases. We don't want to flicker highlighting depending on whether it parsed well or not. Therefore, we are only tokenizing it, which will lead to some minor differences with runtime behavior like in the case of string literals or context-dependent soft keywords (we can only colorize them with limited information, so it won't be 100% correct every time).
The only reason IDLE highlights things differently right now is that it's based on a buncha regexes, which is a thing we actually want to rectify. We will categorically NOT be introducing regexes to colorizing in the REPL.
For example, I wrote the code, but I want to print a string before to debug it. After I input a " or ' the code after were change its color. This effect is bad.
I disagree this is bad. IPython behaves the same, VScode behaves the same.
There is no qualitative difference between an invalid string being colored as a string versus other tokens. It's just as incorrect for Python.
The reason PyREPL colors it like that is because f-strings can be multiline now if split on an expression and, again, we are not parsing, just tokenizing:
The reason PyREPL colors it like that is because f-strings can be multiline now if split on an expression and, again, we are not parsing, just tokenizing:PyREPL
So that if it is not f-string or t-string, it shouldn't colorize like that.
The f-string and t-string have different analysis with normal string.
Activity
StanFromIreland commentedon Oct 20, 2025
Note that this is just an idea for IDLE, not something that has been implemented.
zahlman commentedon Oct 20, 2025
This is about the REPL; IDLE works according to OP's expectations.
In either case, the code has to be pasted in to demonstrate the problem — otherwise, the interpreter will report a syntax error as soon as the Enter key is pressed after the opening quotation mark.
However, in IDLE the invalid pasted code will show up with invalid highlighting: the intended contents of the string literal have the default colour rather than string-literal colour. (The opening quote will, however, only be highlighted after pressing Enter.)
At the REPL, meanwhile, the pasted code is coloured as if it were a valid string literal.
I'm not sure that this is particularly important, since proper feedback is almost immediate and even delaying that feedback a little bit requires copying and pasting invalid code from elsewhere. However, it's a strange inconsistency.
As for syntax highlighting on web pages, that is obviously outside of CPython's control. Every web page author can make the code appear with whatever styling is desired, even if it's misleading, and there's nothing that CPython could do about that. (As far as I'm aware, the CPython documentation doesn't show invalid string literals like this, except specifically to explain that they are invalid.)
terryjreedy commentedon Oct 20, 2025
While there may be a preference for how syntax erroneous code is colorized, there is no correct way. I believe @ambv has said something similar. There is no violation of even an implied specification. Hence, I do not see a bug here and am removing the bug label.
That said, I cannot reproduce the claimed REPL behavior. After copying the code above and running with the same binary, each line is run separately, as with the old REPL.
Ditto in Command Prompt with 3.15 (main). Is there a way to change REPL paste behavior to be more like IDLE's?
Locked-chess-official commentedon Oct 21, 2025
May be use "Ctrl + shift + V"?
Another way is here:
However, now the way to draw the code makes this code look "valid". Maybe it is not a good idea.
Locked-chess-official commentedon Oct 21, 2025
In fact, there is another problem in it: the syntax label is unstable in this absolute invalid syntax.
For example, I wrote the code, but I want to print a string before to debug it. After I input a
"or'the code after were change its color. This effect is bad.StanFromIreland commentedon Oct 21, 2025
On Linux, it is not run till one presses Enter, so I can reproduce the reported colorizing.
picnixz commentedon Oct 21, 2025
It's not just for strings that we have this disparity. For instance, on Linux:
In the REPL, I get (before hitting ENTER):
and once I hit ENTER, I will have a SyntaxError. On the other hand, in IDLE (before hitting ENTER), I get:
When someone C/C in the REPL, it gets colorized independently of whether it's legit code or not, while in IDLE, it gets colorized but not everything. I don't know if it's possible to have a common implementation for colorizing inputs by REPL/IDLE before hitting ENTER (I would say yes since they should only depend on the input lines that could be tokenizied in one go and then colorized).
In some situations, it makes sense to keep highlights (for instance, in my example, if I were to change
1 + 1to a legitimate import, but keepwrong =, with colorization, I would see thatwrong =should be removed but without it, it would be less explicit).StanFromIreland commentedon Oct 21, 2025
We are working on that:-) See the PR linked in the issue description.
zahlman commentedon Oct 21, 2025
I guess you're referring to #140337, now linked from #140347 (not here — until now ;) )?
StanFromIreland commentedon Oct 21, 2025
Apologies I was on mobile and I could not provide a link, but this hyperlink the original post leads me to it?
ambv commentedon Oct 22, 2025
Is there anything actionable here? This behavior is intentional: we are not parsing the input as it will be partial (i.e. incomplete, therefore "invalid") in most cases. We don't want to flicker highlighting depending on whether it parsed well or not. Therefore, we are only tokenizing it, which will lead to some minor differences with runtime behavior like in the case of string literals or context-dependent soft keywords (we can only colorize them with limited information, so it won't be 100% correct every time).
The only reason IDLE highlights things differently right now is that it's based on a buncha regexes, which is a thing we actually want to rectify. We will categorically NOT be introducing regexes to colorizing in the REPL.
I disagree this is bad. IPython behaves the same, VScode behaves the same.
Locked-chess-official commentedon Oct 22, 2025
No. Both pycharm and vscode won't color the wrong string in this way. It showed in the discussion
To prove it:
ambv commentedon Oct 22, 2025
There is no qualitative difference between an invalid string being colored as a string versus other tokens. It's just as incorrect for Python.
The reason PyREPL colors it like that is because f-strings can be multiline now if split on an expression and, again, we are not parsing, just tokenizing:
Locked-chess-official commentedon Oct 22, 2025
So that if it is not f-string or t-string, it shouldn't colorize like that.
The f-string and t-string have different analysis with normal string.