spyder: Indentation handling when running code cells (IndentationError: unexpected indent)

Issue Report Checklist

  • Searched the issues page for similar reports
  • Read the relevant sections of the Spyder Troubleshooting Guide and followed its advice
  • Reproduced the issue after updating with conda update spyder (or pip, if not using Anaconda)
  • Could not reproduce inside jupyter qtconsole (if console-related)
  • Tried basic troubleshooting (if a bug/error)
    • Restarted Spyder
    • Reset preferences with spyder --reset
    • Reinstalled the latest version of Anaconda
    • Tried the other applicable steps from the Troubleshooting Guide
  • Completed the Problem Description, Steps to Reproduce and Version sections below

Problem Description

A cell with indented code cannot be run as it could before. In this example the second cell cannot be run:

print ('hello')

if 1:
    print('hello again')

#%%
    print ('hello')

Versions

Spyder version: 4.1.2
Python version: 3.7.2 64b
Qt version: 5.9.6
PyQt version: 5.9.2
Operating System name/version: Win7 64b

Dependencies

# Mandatory:
atomicwrites >=1.2.0           :  1.2.1 (OK)
chardet >=2.0.0                :  3.0.4 (OK)
cloudpickle >=0.5.0            :  0.6.1 (OK)
diff_match_patch >=20181111    :  20181111 (OK)
intervaltree                   :  None (OK)
IPython >=4.0                  :  7.2.0 (OK)
jedi =0.15.2                   :  0.15.2 (OK)
nbconvert >=4.0                :  5.3.1 (OK)
numpydoc >=0.6.0               :  0.8.0 (OK)
paramiko >=2.4.0               :  2.4.2 (OK)
parso =0.5.2                   :  0.5.2 (OK)
pexpect >=4.4.0                :  4.7.0 (OK)
pickleshare >=0.4              :  0.7.5 (OK)
psutil >=5.3                   :  5.4.8 (OK)
pygments >=2.0                 :  2.3.1 (OK)
pylint >=0.25                  :  2.2.2 (OK)
pyls >=0.31.9;<0.32.0          :  0.31.9 (OK)
qdarkstyle >=2.8               :  2.8 (OK)
qtawesome >=0.5.7              :  0.5.7 (OK)
qtconsole >=4.6.0              :  4.6.0 (OK)
qtpy >=1.5.0                   :  1.5.2 (OK)
rtree >=0.8.3                  :  0.8.3 (OK)
sphinx >=0.6.6                 :  1.8.2 (OK)
spyder_kernels >=1.9.0;<1.10.0 :  1.9.0 (OK)
watchdog                       :  None (OK)
zmq >=17                       :  17.1.2 (OK)

# Optional:
cython >=0.21                  :  0.29.2 (OK)
matplotlib >=2.0.0             :  3.0.2 (OK)
numpy >=1.7                    :  1.15.4 (OK)
pandas >=0.13.1                :  0.24.2 (OK)
scipy >=0.17.0                 :  1.2.1 (OK)
sympy >=0.7.3                  :  1.3 (OK)

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 25 (17 by maintainers)

Most upvoted comments

I think that allowing codecells with arbitrary initial indent is fine.

I agree.

We could even implement it ourselves

@bcolsen, could you create a PR for this?

@dalthviz The only indentation flexibility that ipython has is if code has an indentation in the first line that indentation is removed from all the other lines. This also isn’t corrected in .ipy files because they don’t support executing codecells we are tagging that on ourselves.

As @impact27 mentions this, is the solution:

Why? Random indentation is not correct python code

It also not correct in ipy files either. This behavior is actually most important in python files particularly for the code cells in the if __name__ = "__main__" section.

Fair, then we can use leading_indent from IPython.core.inputtransformer2 (PY3 only) when running cells and the full TransformerManager for ipython files.

We could even implement it ourselves: https://github.com/ipython/ipython/blob/0bc26f65bc775082dc36d89edc1657be031b9e79/IPython/core/inputtransformer2.py#L34

I think that allowing codecells with arbitrary initial indent is fine. I don’t think we need to be that strict about it. It will still give plenty of indentation errors:

In [4]:     print("hello")
   ...: print("hello")
hello
hello

In [5]:     print("hello")
   ...: print("hello")
   ...:         print("hello")
  File "<ipython-input-5-ed6671ba29f1>", line 3
    print("hello")
    ^
IndentationError: unexpected indent


In [6]:     print("hello")
   ...:         print("hello")
   ...:         print("hello")
  File "<ipython-input-6-e529d318b1ea>", line 2
    print("hello")
    ^
IndentationError: unexpected indent


In [7]: print("hello")
   ...:     print("hello")
   ...:     print("hello")
  File "<ipython-input-7-e529d318b1ea>", line 2
    print("hello")
    ^
IndentationError: unexpected indent


In [8]:     print("hello")
   ...:     print("hello")
   ...:     print("hello")
hello
hello
hello

In [9]:         print("hello")
   ...:     print("hello")
   ...:     print("hello")
  File "<ipython-input-9-e529d318b1ea>", line 2
    print("hello")
    ^
IndentationError: unexpected indent

Thanks! The upgrade changes the error to a warning, and now if I rename the file to .ipy the warning goes away.

How will it be in the future?

As @bcolsen observed above:

It also not correct in ipy files either. This behavior is actually most important in python files particularly for the code cells in the if name = “main” section.

What is the motivation for disallowing the execution of indented code blocks? Has it caused problems?

I think some ideas were indeed proposed by @impact27 what do you think @ccordoba12 ?

Let’s wait to see what @adriesse says because I can’t reproduce this bug either on Linux.

Why? Random indentation is not correct python code

It also not correct in ipy files either. This behavior is actually most important in python files particularly for the code cells in the if __name__ = "__main__" section.

Fair, then we can use leading_indent from IPython.core.inputtransformer2 (PY3 only) when running cells and the full TransformerManager for ipython files.