Skip to content

Problem with indentation in the run_cell method #11594

Closed

Description

@grumpfou

Hi,

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.
image

Tested on JupyterNotebook, JupyterLab and the Ipython interactive shell in the terminal.

Config:

  • jupyter --version 4.4.0
  • ipython --version 7.0.1
  • python3 --version Python 3.6.7
  • Ubuntu 18.04.1 LTS

Activity

  1. Carreau commented on Feb 7, 2019

    @Carreau
    Member

    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.

  2. rgson commented on May 7, 2019

    @rgson
    Contributor

    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.

  3. Carreau commented on May 10, 2019

    @Carreau
    Member

    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.

  4. rgson commented on May 10, 2019

    @rgson
    Contributor

    @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.

  5. added this to the 7.6 milestone on May 26, 2019
  6. added a commit that references this issue on Jun 1, 2019
    23189f7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions