Repository navigation
Improve PyREPL auto-indentation #133710
Description
Bug report
On second thought this issue should be an enhancement instead of a bug report. Sorry for the wrong template.
Bug description:
There are some cases where _pyrepl auto-indentation works not well.
Cases
- A line ending with
:in a multi-line string is wrongly indented.
Observed
>>> s = '''
... Note:
... ␣␣␣␣|Expected
>>> s = '''
... Note:
... |#inside strings is seen as a comment, the following:is ignored.
Observed
>>> if ' ' == '#':
... |Expected
>>> if ' ' == '#':
... ␣␣␣␣|- When the entire cursor line is a comment and is already indented, pressing Enter gives a further indent.
Observed
>>> def f():
... # foo⤶
... ␣␣␣␣␣␣␣␣|Expected
>>> def f():
... # foo⤶
... ␣␣␣␣|Possible solution
Currently _should_auto_indent() parses the buffer from right to left and stops at the first newline it encounters. Only the last line that is not a comment line of the buffer is parsed.
But by parsing from right to left we can't tell if a # starts a comment or is part of a string. For example if we, from right to left, encounter a " first and then a #, we don't know if the # is a comment. To know that, we need to know if the " is a string boundary, but the # might comment out the ", so we can't be sure. There is a information dependency cycle.
To fix this I made a change to parse the buffer from left to right, keeping track of whether current char is inside a string or a comment. This approach solves the above three cases. However the whole buffer is parsed on every call of _should_auto_indent(), with very long buffer, there might be noticeable delay when pressing Enter.
I think this is a big change. It affects how _should_auto_indent() works as a whole. I am hesitated to create a PR and just put it here first to hopefully get feedback.
CPython versions tested on:
3.15
Operating systems tested on:
Linux
Activity
[-]PyREPL auto-indent not work in two cases[/-][+]PyREPL auto-indent not work in multi-line string and when '#' is literal string instead of comment[/+][-]PyREPL auto-indent not work in multi-line string and when '#' is literal string instead of comment[/-][+]PyREPL auto-indent not work in multi-line string, when '#' is literal string instead of comment, and comment line below function definition[/+][-]PyREPL auto-indent not work in multi-line string, when '#' is literal string instead of comment, and comment line below function definition[/-][+]Improve PyREPL auto-indentation[/+]pablogsal commentedon Oct 30, 2025
I think that this may unfortunately be a deal breaker. Thoughts @ambv ?
tanloong commentedon Oct 30, 2025
Thanks for the response! I will see if I can profile it over the weekend. If it ends up causing significant lag, what do you think about left-to-right full parsing only when the buffer is under certain threshold length, and falling back to the original right-to-left partial parsing for longer buffer?
ambv commentedon Oct 31, 2025
I would prefer to keep the implementation simple and not split behavior based on input size. What problem are we solving here exactly? Just that auto-indent works inside multiline strings?
tanloong commentedon Nov 9, 2025
@ambv Apologies for the split behavior suggestion, I agree it would complicate the implementation unnecessarily.
@pablogsal I've profiled the performance impact of the change. Here's a comparison between the original and enhanced versions when processing a multiline string with up to 10k characters. While the relative slowdown is significant, the absolute delay remains low (~0.02s for 10k characters on an i5-7200U 2.5GHz CPU with an HDD). The delay is not noticeable, I think, and in interactive REPL scenarios the inputs would be shorter.
The code:
The result: