Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"IndentationError: unexpected indent" when evaluating indented blocks of code #2837

Closed
bodkan opened this issue Oct 10, 2018 · 21 comments
Closed
Assignees
Labels
area-terminal feature-request Request for new features or functionality

Comments

@bodkan
Copy link

bodkan commented Oct 10, 2018

Environment data

  • VS Code version: 1.27.2
  • Extension version: 2018.9.0
  • OS and version: macOS Sierra 10.12.6
  • Python version: 3.7.0, installed via pyenv
  • Type of virtual environment used: none
  • Relevant/affected Python packages and their versions: none

Actual behavior

Sending a selected Python code for evaluation in the Python terminal via Run Selection/Line in Python Terminal results in "IndentationError: unexpected indent" errors if the block of code is indented.

Example: evaluating the following selection results in an error because of the indentation of the whole block at 4 spaces:

image

image

Expected behavior

It would be useful if the evaluation of indented code worked regardless of it's position or level of indentation in the overall code (i.e. should be first deindented to a "base level" before sending to the terminal?).

As an example, the indented code above should be sent to the terminal as if it was the following (i.e. not indented):

image

image

I tend to work "bottom-up", evaluating/debugging smaller pieces of code that form more complex functions, and those individual pieces are obviously indented. This behavior makes this sort of workflow a little difficult.

Steps to reproduce

Use the following example code to test the behavior described above by evaluating the two commented blocks using Run Selection/Line in Python Terminal functionality.

def fn():
    # block indented by 4 spaces -> results in an error
    x, y = 10, 2
    for i in range(x):
        print(i ** y)

# block not indented -> correctly evaluated
x, y = 10, 2
for i in range(x):
    print(i ** y)
@d3r3kk d3r3kk added bug Issue identified by VS Code Team member as probable bug needs verification area-terminal labels Oct 10, 2018
@bodkan
Copy link
Author

bodkan commented Oct 10, 2018

I just briefly looked for a potential solution, at it seems that the issue could be solved by adding something like this as one of the pre-processing steps in this function?

# Step X:  Remove excess indentation
indentation = re.search(r'^\s+', lines[0]).group()
if indentation:
    lines = [re.sub(indentation, '', l, count=1) for l in lines]

I have no experience with the codebase of this extension, but I'm happy to poke around more if you think this is something worth fixing.

@DonJayamanne
Copy link

Before looking into this, we'd first need to fix #169

@syagev
Copy link

syagev commented Nov 14, 2018

A quick n dirty workaround for now is to "multi-select" the code block using ALT+SHIFT:
image

That still won't let you right-click > Debug:Evaluate but you can copy paste it in the debug window to get the code without any leading indentations.

@DonJayamanne DonJayamanne removed their assignment Dec 6, 2018
@tjaeel
Copy link

tjaeel commented Jan 8, 2019

SOLVED:

Been struggling with this for an hour. Something this simple wouldn't run:

image

SOLUTION: Just make sure that the LAST line you run when using (Shift + Enter) is not indented. If the last line is indented, you will either get an indentation error or be shown your code again in output

SOLUTION EXAMPLE (this runs perfectly):

image

@DonJayamanne please fix this, thanks =)

@martinpeck
Copy link
Member

User has seen this issue and posted a question related to it on Stack Overflow. I assume this issue is the cause of this user's problems.

https://stackoverflow.com/questions/54276596/indentation-error-on-visual-studio-code-on-a-mac-again

@nihirisuto
Copy link

Python is 100% unusable for me because of this, I have code that executes flawlessly in terminal, pycharm, spyder, jupyter, thonny, etc but nothing but indentation exceptions are being thrown in VSCode. The best part? I wrote it all in VSCode. It was executing fine and it started to get worse and worse, and now the code just doesn't even wanna go.

Disabled PyLint and it worked once, but now the behavior is back to indentation errors

@scorpiocavalier
Copy link

This worked for me:

In Visual Studio 2017, go to Tools > Options > Text Editor > Python > Tabs.
Select "Keep tabs" and click OK.

Erase the current white spaces and indent as usual with Tab key.

The red lines should disappear.

@DonJayamanne DonJayamanne added needs decision feature-request Request for new features or functionality and removed needs PR bug Issue identified by VS Code Team member as probable bug labels Feb 25, 2019
@qubitron qubitron added needs proposal Need to make some design decisions and removed needs decision labels Mar 5, 2019
@qubitron
Copy link

qubitron commented Mar 5, 2019

We would need to figure out how to handle cases where you want to intentionally paste indented blocks of code, or how many people would be unhappy if this was the default behavior.

@DougPlumley
Copy link

We would need to figure out how to handle cases where you want to intentionally paste indented blocks of code, or how many people would be unhappy if this was the default behavior.

I think the issue is primarily when you're running a selection, not pasting code.

The current behavior is rough, basically have to copy and paste code elsewhere to run it. One of the many reasons I hate how Python enforces code structure.

@jjotterson
Copy link

I am having the same problem but only for the VS Code in my mac. I got some code that I wrote in a windows vs code (no problems there), and when opening it on my mac vs code, I cannot run any block (using shift + enter).

@jjnurminen
Copy link

jjnurminen commented Apr 4, 2019

The Spyder IDE handles 'run selection' fine, apparently by just unindenting the block so that the lowest indentation level is 0. IMO, this should the behavior in VSCode too. (Also, a proper IPython shell with workable history and command completion would be great! The current REPL and Jupyter notebook are somewhat lacking.)

@gcarmiol
Copy link

Hello, I code in Spyder and PyCharm and both accept by default sending indented code to the console/interactive window and long as it's a selection of code or single line. I believe also that this needs to be the default when sending sections of code to be executed. What needs to be done is that any time a block of code is sent to the interactive window, the lowest indent becomes the "no indent" and it's all aligned according to this lowest indent.
I also want to move to VSCode but this is making it hard. Right now I have to select a block....press Shift+Tab until I get it to the edge of the window, execute it....then tab it back to it's original indent.

@kaseyriver11
Copy link

kaseyriver11 commented May 6, 2019

We would need to figure out how to handle cases where you want to intentionally paste indented blocks of code, or how many people would be unhappy if this was the default behavior.

@qubitron Is this possible? I just want to run indented lines of code like I can in pycharm. For example, when testing functionality within a definition. I don't want to run the whole definition, just 1-2 lines of code. I don't want to unindent, run, and reindent.

@rubenl92
Copy link

rubenl92 commented May 8, 2019

Same issue as @gcarmiol
I also want to move to VSCode but this is making it hard. Right now I have to select a block....press Shift+Tab until I get it to the edge of the window, execute it....then tab it back to it's original indent.

Moreover... shift+tab and tab back is also giving me problems with flake8 indentation.
After i tab back i get: 'continuation line over-indented for visual indent' from flake8.

Can someone please post a proper workaround or pick up this issue?

Thank you so much in advance!

@nsymms
Copy link

nsymms commented May 8, 2019

I completely agree with all of this. I probably select-and-execute code blocks a hundred times a day with pycharm or spyder. And it's impossible with VSCode.

And I'm beginning to think I'm doing something wrong since there's so few complaints about this issue. What is everybody else doing? What debug workflow are others doing that doesn't hit this issue? Isn't everybody's code indented? Every Python script I write has almost every line indented except for the import lines at the top and the one if __name__ == '__main__' line.

@jjnurminen
Copy link

jjnurminen commented May 9, 2019

What is everybody else doing? What debug workflow are others doing that doesn't hit this issue?

Funny that you mentioned debugging, since for me the integrated debugger is basically the only reason to use VSCode. It saved me on many occasions where I needed a proper debugger.

Otherwise, I have to say that VSCode-Python is pretty clunky: it's slow, there's no proper interactive shell, running code selections is pretty much impossible, the language server is still unusable and many operations just keep failing randomly (e.g. 'Run Python file in Terminal') works about 50% of the time for me).

However it does look promising. I keep hoping that it'll be more usable in the near future.

@luabud luabud added needs PR and removed needs proposal Need to make some design decisions labels May 9, 2019
@luabud
Copy link
Member

luabud commented May 9, 2019

Always de-dent to the first non-empty line in the selection

@mikful
Copy link

mikful commented May 23, 2019

I totally agree with the points raised. This issue makes working with Python files in VS Code a real bore. Otherwise, it runs much faster for me than Spyder and I would love to keep using it, but this is becoming a real issue when evaluating sections of larger code chunks.

@DonJayamanne
Copy link

Prescribed solution:

  • Detent to outer most indentation
  • Modify pythonFiles/normalizeForInterpreter.py (use textwrap.dedent)

@DonJayamanne DonJayamanne added this to the 2019 - May Sprint 11 milestone May 29, 2019
@nsymms
Copy link

nsymms commented May 30, 2019

Thanks for addressing this, @DonJayamanne.

I think proposed solution is probably the best that can be done given the current architecture. The only thing missing (IMHO) would be to consider the indentation of the line where the selection starts. Knowing the original indent of the first line (when the entire line is not included in the selection -- e.g. spaces missed, some code parts intentionally omitted) would ensure the remainder of the selection is properly re-indented.

However, looking at the code I can see that only the selected text is passed; examining the original first line from the editor is not feasible without a larger refactor.

@ericsnowcurrently
Copy link
Member

validated

@ghost ghost removed the needs PR label Jun 5, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Jun 12, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-terminal feature-request Request for new features or functionality
Projects
None yet
Development

No branches or pull requests