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
Problem with indentation in the run_cell method #11594
When using the InteractiveShell method run_cell, there is a problem when the string to execute begins with a line that contains only 4 spaces. It then does not handle properly the indentations of the subsequent loops.
Tested on JupyterNotebook, JupyterLab and the Ipython interactive shell in the terminal.
Yes, the IPython machinery is attempting to detect leading indent and remove them. This is to handle text that might be copy-pasted from other source (like the web) that may include leading indent.
It will also automatically strip leading >>>, ..., ... : and In [x] prompt.
How about trimming based on the line with the least amount of leading whitespace? Or at least ignoring whitespace-only lines? Seems like that would be less error-prone.
There are a lot of historical reason for the current behavior; one of them is that often when copy-pasting from the web the first line does not have the same amount of whitespace than the others. So looking at the line with the least WS may not work.
I believe it make sens to ignore the first line if it's only whitespace – ignoring all line with only WS may work as well. I'd be happy to look at a PR that does that.
I am able to run the example provided by @grumpfou without errors after the proposed change.
I refrained from removing all blank lines due to the increased complexity of handling e.g. multi-line strings containing blank lines. It shouldn't be necessary, anyway.
Activity
Carreau commentedon Feb 7, 2019
Yes, the IPython machinery is attempting to detect leading indent and remove them. This is to handle text that might be copy-pasted from other source (like the web) that may include leading indent.
It will also automatically strip leading
>>>,...,... :andIn [x]prompt.rgson commentedon May 7, 2019
How about trimming based on the line with the least amount of leading whitespace? Or at least ignoring whitespace-only lines? Seems like that would be less error-prone.
Carreau commentedon May 10, 2019
There are a lot of historical reason for the current behavior; one of them is that often when copy-pasting from the web the first line does not have the same amount of whitespace than the others. So looking at the line with the least WS may not work.
I believe it make sens to ignore the first line if it's only whitespace – ignoring all line with only WS may work as well. I'd be happy to look at a PR that does that.
rgson commentedon May 10, 2019
@Carreau PR submitted.
I am able to run the example provided by @grumpfou without errors after the proposed change.
I refrained from removing all blank lines due to the increased complexity of handling e.g. multi-line strings containing blank lines. It shouldn't be necessary, anyway.
Remove leading empty lines