Skip to content

Remove leading empty lines - #11718

Merged
Carreau merged 2 commits into
ipython:masterfrom
rgson:leading-empty-lines
May 26, 2019
Merged

Carreau merged 2 commits into
ipython:masterfrom
rgson:leading-empty-lines

Conversation

@rgson

@rgson rgson commented May 10, 2019

Copy link
Copy Markdown
Contributor

When running a cell, the leading indentation on the first line is
removed from all of the cell's lines. This causes indentation errors
on subsequent lines in case the first line consists entirely of
whitespace matching a valid level of indentation that is greater than
the least indented line.

This change avoids the issue by first removing any leading,
whitespace-only lines.

Closes #11594

When running a cell, the leading indentation on the first line is
removed from all of the cell's lines. This causes indentation errors
on subsequent lines in case the first line consists entirely of
whitespace matching a valid level of indentation that is greater than
the least indented line.

This change avoids the issue by first removing any leading,
whitespace-only lines.

Closes #11594
@Carreau Carreau added this to the 7.6 milestone May 11, 2019
@Carreau

Carreau commented May 11, 2019

Copy link
Copy Markdown
Member

Thanks looks good to me.
I'm still going to ask thoughts of @takluyver who wrote most of this.

@Carreau
Carreau requested a review from takluyver May 11, 2019 21:52
Comment thread IPython/core/tests/test_inputtransformer2_line.py Outdated
@takluyver

Copy link
Copy Markdown
Member

Thanks, other than the point about testing, this looks OK to me.

@Carreau
Carreau merged commit 0e052ba into ipython:master May 26, 2019
@ghazpar

ghazpar commented May 3, 2023 •

Copy link
Copy Markdown

I understand the interest of being able to paste code into a notebook cell and run it without having to fix the indentation, but there are also some downside to this kind of hack:

  1. A Python beginner might think that such indentation is valid syntax.
  2. If I copy such cell content that runs fine in a notebook and than paste it somewhere else, I will get a syntax error, which can again be confusing to a beginner, and frustrating to a non-beginner.

I don't think its a good idea that a notebook cell be allowed to run Python code with invalid indentation.

If there is really a need to tolerate superfluous indentation when pasting, the indentation should be corrected automatically during the paste process so that if the cell runs without error, it's because the cell contains valid Python syntax.

And such automatic correction behavior should ideally be optional.

Just my 2c.

@Carreau

Carreau commented May 3, 2023

Copy link
Copy Markdown
Member

Happy to make it optional, but someone would have to send patches to IPython/IPykernel to have the relevant configuration hooks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Problem with indentation in the run_cell method

4 participants