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

Python3.13 All functions fail to send: IndentationError: unexpected indent #24256

Closed
karthiknadig opened this issue Oct 5, 2024 Discussed in #24222 · 38 comments · Fixed by #24401
Closed

Python3.13 All functions fail to send: IndentationError: unexpected indent #24256

karthiknadig opened this issue Oct 5, 2024 Discussed in #24222 · 38 comments · Fixed by #24401
Assignees
Labels
area-repl bug Issue identified by VS Code Team member as probable bug verification-needed Verification of issue is requested verified Verification succeeded
Milestone

Comments

@karthiknadig
Copy link
Member

Discussed in #24222

Originally posted by ddxv October 2, 2024
I recently fired up Python3.13rc2 but quickly found that any lines I send to the terminal do not work, specifically anything with additional line breaks or with indentation.

The new REPL, as I'm sure you know, autoindents for users. So it appears I'm getting a new indent for each line + the existing indent, causing a deeper and deeper indent even on a flat function. As you can see in the screenshot:
image

This inevitably leads to: IndentationError: unexpected indent

I'm opening a discussion because I don't see others mentioning this for Python 3.13, so I was not sure if this is actually a bug or something unique to my settings?

If others are encountering this, happy to turn this into a ticket.

@karthiknadig karthiknadig added the bug Issue identified by VS Code Team member as probable bug label Oct 5, 2024
@github-actions github-actions bot added the triage-needed Needs assignment to the proper sub-team label Oct 5, 2024
@AlexNaglich
Copy link

I am experiencing the same.

Version: 1.94.2 (system setup)
Commit: 384ff7382de624fb94dbaf6da11977bba1ecd427
Date: 2024-10-09T16:08:44.566Z
Electron: 30.5.1
ElectronBuildId: 10262041
Chromium: 124.0.6367.243
Node.js: 20.16.0
V8: 12.4.254.20-electron.0
OS: Windows_NT x64 10.0.26100
python: 3.13.0

@MMateo1120
Copy link

Same behaviour and error, only happens with python 3.13, 3.12 works fine.

@anthonykim1
Copy link

Thanks for the bug report! We investigate issues in order based on priority and severity, which includes the impact it has on your ability to use the extension to do productive work, and the number of people affected. If other users come forward and leave a comment demonstrating they are seeing/reproducing the problem then we will raise this issue's priority. Thanks for understanding and the patience!

Please go ahead and upvote this meanwhile so that we can further acknowledge the severity.

@AlexNaglich
Copy link

AlexNaglich commented Oct 17, 2024

Regarding severity, it greatly inhibited my ability to do work. Testing individual blocks of code is no longer a viable option for me. I cannot copy paste, I cannot shift+enter, I cannot right click and run in interactive window... I have to modify the python code, then to non-interactively test I run the entire program that calls the python. Dramatic decrease in productivity as a result of tedious testing and debugging.

@teuncm
Copy link

teuncm commented Oct 18, 2024

For me and students of our classes the impact is also huge. We teach the first steps of Python programming by letting them run blocks of code using shift+enter, but with the new behavior this is no longer an option at all.

@cbrnr
Copy link

cbrnr commented Oct 24, 2024

I just wanted to add that this also severely impacts how I use Python 3.13 in Visual Studio Code. For those looking for a workaround (other than downgrading Python to ≤ 3.12 or setting the env variable PYTHON_BASIC_REPL=1, which are both not good options), is to use the "Native REPL" (enable in the settings).

@cbrnr
Copy link

cbrnr commented Oct 24, 2024

BTW, just to let Windows users know that this currently does not work in Windows Terminal either, see python/cpython#124119.

@masai2018
Copy link

I updated VS Code to the most updated version (1.95.0) but still have the same error with Python 3.13 on Windows and Mac OS.

@anthonykim1
Copy link

Thanks @cbrnr for

(other than downgrading Python to ≤ 3.12 or setting the env variable PYTHON_BASIC_REPL=1, which are both not good options), is to use the "Native REPL" (enable in the settings).

Thanks everyone for the feedback here. Team is actively looking into resolving this, but please try out the "Native REPL" setting to send code and execute in Native REPL instead of terminal meanwhile.

@anthonykim1 anthonykim1 added this to the November 2024 milestone Oct 29, 2024
@anthonykim1
Copy link

@cbrnr The type of indentation error we can get seems very similar from what you mentioned python/cpython#124096 (issue from referenced PR). I think the indentation problem is not contained for just windows as we are experiencing for MacOS and linux too.

@benichou
Copy link

benichou commented Nov 6, 2024

I am having the same issue and would hope to see a fix on python 3.13 with integration to VSCode. Reverted to python 3.12 and no problems

Hoping @anthonykim1 you will be to look into this with your team,

Appreciate the help

@anthonykim1
Copy link

anthonykim1 commented Nov 8, 2024

Hey folks, good news here:

I've resolved the issue for 3.13 on this PR: #24401
That being said, please try Python extension's pre-release on VS Code insiders starting Monday afternoon. (Try late afternoon on Monday or Tuesday would be even safer)

Edit: /cc @benichou apologies for confusion here, we don't release pre-release on weekend so it will build Monday morning and will be available Monday afternoon, or Tuesday if that is easier for you.

Let me know how this goes for your specific operating system (Mac, Windows, Linux)
Thanks!

@benichou
Copy link

benichou commented Nov 8, 2024 via email

@MMateo1120
Copy link

MMateo1120 commented Nov 11, 2024

Using the pre-lease verion of python extension (v2024.19.2024111101 (pre-release)), it still does not work, gives the same error:

Image

runs in terminal as:

Image

@anthonykim1
Copy link

https://github.com/user-attachments/assets/2b72da8e-ca04-447e-8e14-7eaed1c6bd9d
I can't seem to repro. Can you all restart extension after updating and also run command "Python Clear Cache and Reload" after you exited out of all python repl?

@cbrnr
Copy link

cbrnr commented Nov 12, 2024

I just tried again with the regular release (no preview), and the indentation is now correctly transferred to the REPL.

However, I do get a

KeyboardInterrupt

before every executed command:

def f(x):
    if x > 0:
        return 1
    elif x < 0:
        return -1
    else:
        return 0

print(f(42))

Running these two commands with Shift-Enter results in:

>>> 
KeyboardInterrupt
>>> def f(x):
...     if x > 0:
...         return 1
...     elif x < 0:
...         return -1
...     else:
...         return 0
... 
>>> 
KeyboardInterrupt
>>> print(f(42))
1

@anthonykim1
Copy link

anthonykim1 commented Nov 12, 2024

Thanks much for trying this out @cbrnr :)
Yes, that KeyboardInterrupt is handled via #24422
So you give it a try(latest PRE-RELEASE of Python extension on VS Code INSIDERS) tomorrow (it just got merged today Nov 12), and it wont be there!

Let me know if you encounter any problems.
Thanks again

@cbrnr
Copy link

cbrnr commented Nov 13, 2024

Thanks, now the KeyboardInterrupt is gone and everything works fine (although I didn't test on Windows, maybe someone else can confirm that)!

@cbrnr
Copy link

cbrnr commented Nov 14, 2024

I'm not sure if I'm doing something wrong, but on Windows, hitting Shift-Enter on a line of code does not even start the interactive interpreter. It just pastes the current line into the Terminal, which is running PowerShell. I don't usually work on Windows, but I've synced my VSC settings, which do work on macOS and Linux. Maybe some keyboard shortcut conflict? Both "Python: Run Selection/Line in Python REPL" and "Python: Run Selection/Line in Python Terminal" are bound to Shift-Enter.

OK, "Python: Run Selection/Line in Python Terminal" is the correct command, and disabling the other at least runs the selected block of code in the interpreter. Not sure what the difference between these two commands is though.

However, on Windowsit still doesn't work, because every line is getting additional indentation probably due to python/cpython#124119.

@anthonykim1
Copy link

Hey @cbrnr Thanks much for testing this thoroughly :)

The difference is the Python Terminal one is supposed to send code to your terminal REPL (one with >>> while the Python REPL one sends code to native REPL https://code.visualstudio.com/docs/python/run#_native-repl

Im not sure if you manually changed your shortcut, but the shift+enter behavior should change dynamically depending on your setting:
enabling send to native repl setting should send code to your "Python REPL" aka. native repl, otherwise it will send to your terminal REPL (>>>)
Perhaps there is difference in your workspace and user setting??

Let me know how things go with playing around with these settings.

And yes you are absolutely correct for:

However, on Windowsit still doesn't work, because every line is getting additional indentation probably due to python/cpython#124119.

@cbrnr
Copy link

cbrnr commented Nov 14, 2024

No problem, I'm using Visual Studio Code and the Python Extension for teaching, that's why I keep finding and reporting things 😄.

I have also asked this in a discussion thread #24443. I'll answer over there since this issue is already closed (and my comment is not even related to this issue here).

@anthonykim1
Copy link

For anyone interested in more discussions for native repl (new editor based vscode native python repl) vs. terminal repl (interactive interpreter with >>>). Here is the link: #24443

For rest of other folks the indentation should be fixed on the latest pre-release version of Python extension on VS Code Insiders :)
Let me know or file new issues if problem persists. Thanks

@MMateo1120
Copy link

MMateo1120 commented Nov 15, 2024

I am still experiencing the issue, the same way as before: #24256 (comment)

I uninstalled every extension but python's:
Image

I am using the pre-lease version:
Image

the python version of 3.13.0 64-bit:
Image

Started a new window, a brand new py file but after shift+enter, the same behaviour is found as before:
Image

Also, if i ctrl c ctrl v the code into the terminal, extra indents are added just by the copying of the code into the terminal:
Image

As for others the error is fixed (or at least for one people with feedback), could you give any advice in what I could do to make this work?

Thanks

@cbrnr
Copy link

cbrnr commented Nov 15, 2024

Are you on Windows? There's another bug which affects only Windows (linked in a previous comment).

@srdgtr
Copy link

srdgtr commented Nov 25, 2024

on fedora 41 with Python 3.13.0 (main, Oct 8 2024, 00:00:00) [GCC 14.2.1 20240912 (Red Hat 14.2.1-3)] with visual studio code 1.95.3 and pylance v2024.11.3 I still see it happening:
" Python 3.13.0 (main, Oct 8 2024, 00:00:00) [GCC 14.2.1 20240912 (Red Hat 14.2.1-3)] on linux
Type "help", "copyright", "credits" or "license" for more information.

KeyboardInterrupt

try:
... import requests
... except ModuleNotFoundError as ve:
... print(f"{ve} trying to install")
... install("requests")
... import requests
...
File "", line 3
except ModuleNotFoundError as ve:
^^^^^^
SyntaxError: invalid syntax
"

@JordyScript
Copy link

Seems like the pre release version of the Python Extension has fixed it for me v2024.21.2024112701 (pre-release)

VS Code Version: 1.95.3
MacOS 15.1.1
Python 3.13.0

@anthonykim1
Copy link

@srdgtr You should try it on latest VS Code Insiders and latest version of Python extension PRE-RELEASE.

@anthonykim1
Copy link

anthonykim1 commented Dec 2, 2024

Fixes for indentation error should be effective for latest VS Code insider and latest pre-release of Python extension for MacOS & Linux

FYI: Windows users with Python3.13 (regardless of VS Code terminal or external terminal) are still suffering from indentation error from: python/cpython#124096

If you are on mac or linux please make sure indentation error has been resolved when using Python REPL in VS Code terminal.
If you are on Windows, PLEASE verify you are suffering from indentation error both in VS Code terminal and external terminal (terminal outside of VS Code)

Verification steps (Mac and Linux)

  1. Install latest pre-release of VS Code insiders and latest pre-release of Python extension
  2. Turn off setting for native repl (go to setting UI toggle off 'send to native repl' setting for workspace,user settings
  3. shift+enter from your desired Python file (make sure your Python interpreter is set to 3.13)
  4. Make sure indentation are not broken when sent to terminal

Verification steps (Windows)
Pretty much same as above but you should notice indentation are still broken for REPL in terminal inside VS Code AND also external terminal outside of VS Code.

@anthonykim1 anthonykim1 added the verification-needed Verification of issue is requested label Dec 2, 2024
@dbaeumer
Copy link
Member

dbaeumer commented Dec 4, 2024

I failed to install Python 3.13 into my Python dev container using

sudo add-apt-repository ppa:deadsnakes/ppa

Any tips?

@dbaeumer dbaeumer added the verification-steps-needed Steps to verify are needed for verification label Dec 4, 2024
@karthiknadig
Copy link
Member Author

@anthonykim1 anthonykim1 removed the verification-steps-needed Steps to verify are needed for verification label Dec 4, 2024
@dangets
Copy link

dangets commented Dec 4, 2024

@anthonykim1 - using the pre-release version of the plugin, it appears the indentation issue was fixed, but the behavior of selected lines sent to the REPL appears to have changed.

With a selection as pictured:

Image

If I send to the repl with v2024.21.2024112901 (pre-release) the repl doesn't add a remaining newline and is left waiting for input on the block.

Python 3.13.1 (main, Dec  4 2024, 09:22:57) [Clang 16.0.0 (clang-1600.0.26.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import asyncio
... from azure.identity.aio import ClientSecretCredential
... from msgraph import GraphServiceClient
... 

With the release version v2024.20.0 I see the below (but the indentation bug exists)

Python 3.13.1 (main, Dec  4 2024, 09:22:57) [Clang 16.0.0 (clang-1600.0.26.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import asyncio
>>> from azure.identity.aio import ClientSecretCredential
>>> from msgraph import GraphServiceClient
>>> 

@anthonykim1
Copy link

Hi @dangets Thanks for trying this out. This is actually coming from cpython itself - as they have tweaked indentation execution in the new Pyrepl.

Observe:
Image

To get a better understanding of this, you can create a REPL outside of VS Code (external terminal) and then try copying and pasting couple lines of code and you would be able to see how the behavior changed.

Glad that indentation error specific to VS Code was resolved by us :)

@dbaeumer
Copy link
Member

dbaeumer commented Dec 5, 2024

@karthiknadig thanks

@dbaeumer dbaeumer added the verified Verification succeeded label Dec 5, 2024
@dangets
Copy link

dangets commented Dec 6, 2024

@anthonykim1 - I'm not sure I follow your point about the new pyrepl. I was using the same version of python (3.13.1), but only changing the vscode plugin version and seeing different behavior. Your example is switching between python 3.13.0 and 3.12.5

@anthonykim1
Copy link

anthonykim1 commented Dec 6, 2024

@dangets I mean that you should notice the change in behavior (such as trailing ...) when comparing 3.12.5 vs. 3.13.0 REP. You should notice the copy-pasting & indentation behavior has changed from Python itself from 3.12 vs. 3.13

I'm not sure I follow your point about the new pyrepl. I was using the same version of python (3.13.1), but only changing the vscode plugin version and seeing different behavior.

The one you were seeing v2024.20.0 (with broken indentation if you had functions or class in your example), wouldn't be accurate example of experience we are aiming for. You should compare our terminal REPL's behavior with cpython's REPL behavior when comparing indentation and copy and pasting behavior.

@dangets
Copy link

dangets commented Dec 6, 2024

@anthonykim1 - My concern is that there is a regression in the upcoming pre-release version that changes the send-to-repl behavior.

I don't really care about the preceding ... on the repl or if it executes the statements one by one vs. as a block as long as it eventually executes all of the lines, but the problem is that the plugin isn't sending the final newline to the repl to actually trigger the execution given the same editor selection.

With the pre-release version of the plugin - the editor is left hanging and I have to click on the repl and hit enter for it to execute the sent lines. With the existing stable version, I don't have to do this step. I am assuming that this regression was introduced as a result of the change to fix the indentation issue. Again - this is using the same version of python and only changing the vscode plugin version.

@anthonykim1
Copy link

@dangets Thanks for the input, I filed the issue here: #24565
Although REPL in VS Code might deviate a little from cpython REPL itself, it seems more convenient to automatically "execute" to prevent manual "Enter" key presses.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-repl bug Issue identified by VS Code Team member as probable bug verification-needed Verification of issue is requested verified Verification succeeded
Projects
None yet
Development

Successfully merging a pull request may close this issue.