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

autopep8 formatOnSave breaks f-strings on long lines, causing "unterminated string literal" or "EOL while scanning string literal" #22644

Closed
afarah1 opened this issue Dec 13, 2023 · 4 comments
Labels
info-needed Issue requires more information from poster triage-needed Needs assignment to the proper sub-team

Comments

@afarah1
Copy link

afarah1 commented Dec 13, 2023

Type: Bug

Behaviour

Expected vs. Actual

Expected: Upon saving the file, f-strings on long lines are not broken at string literals, or have \ added to the EOL.

Actual: Upon saving the file, f-strings on long lines get incorrect line breaks, causing "unterminated string literal" or "EOL while scanning string literal". If one attempts to fix the line break with a \, autoFormat removes that upon save. It's impossible to have a file with an f-string on a long line, autoFormat has to be disabled.

Steps to reproduce:

  1. Enable formatOnSave with ms-python.autopep8. On settings.json:
    "[python]": {
        "editor.defaultFormatter": "ms-python.autopep8",
        "editor.formatOnSave": true,
    },
  1. Edit and save the following file, triggering auto-format:
mystr = 'abc'
path = f'aVeryLargeFStringLineThatBreak{mystr}aVeryLargeFStringLineThatBreaksWithaPep8'
  1. The file gets formated to:
mystr = 'abc'
path = f'aVeryLargeFStringLineThatBreak{
    mystr}aVeryLargeFStringLineThatBreaksWithaPep8'
  1. Attempt to fix with the following and save again:
mystr = 'abc'
path = f'aVeryLargeFStringLineThatBreak{\
    mystr}aVeryLargeFStringLineThatBreaksWithaPep8'
  1. The modifications are overwritten by autoformat.
  2. Run the file, getting:
$ python3 test.py
  File "/Users/me/test.py", line 2
    path = f'aVeryLargeFStringLineThatBreak{
                                            ^
SyntaxError: EOL while scanning string literal

Diagnostic data

  • Python version (& distribution if applicable, e.g. Anaconda): 3.12.0
  • Type of virtual environment used (e.g. conda, venv, virtualenv, etc.): Poetry
  • Value of the python.languageServer setting: Default

Extension version: 2023.8.0
VS Code version: Code 1.78.0 (252e5463d60e63238250799aef7375787f68b4ee, 2023-05-03T20:15:29.774Z)
OS version: Darwin arm64 23.1.0
Modes:
Sandboxed: No

@github-actions github-actions bot added the triage-needed Needs assignment to the proper sub-team label Dec 13, 2023
@karthiknadig
Copy link
Member

Can you test this by:

cat /Users/me/test.py | python -m autopep8 -

This seems like autopep8 itself is returning this. See issue on autopep8 with f-strings: hhatto/autopep8#712

@github-actions github-actions bot added the info-needed Issue requires more information from poster label Dec 13, 2023
@afarah1
Copy link
Author

afarah1 commented Dec 13, 2023

You're correct, I didn't know about that issue. Please disregard my report.

@afarah1 afarah1 closed this as completed Dec 13, 2023
@github-actions github-actions bot removed the info-needed Issue requires more information from poster label Dec 13, 2023
@afarah1
Copy link
Author

afarah1 commented Dec 13, 2023

Actually, strike that... The file was already formatted in vscode when I tested directly with autopep8... Sorry for the confusion.

It seems like autopep8 doesn't break the line at all:

$ cat test.py 
mystr = 'abc'
path = f'aVeryLargeFStringLineThatBreak{mystr}aVeryLargeFStringLineThatBreaksWithaPep8'

$ cat test.py | python3 -m autopep8 -
mystr = 'abc'
path = f'aVeryLargeFStringLineThatBreak{mystr}aVeryLargeFStringLineThatBreaksWithaPep8'
$ cat test2.py 
mystr = 'abc'
path = f'aVeryLargeFStringLineThatBreak{mystr}aVeryLargeFStringLineThatBreaksWithaPep8'
long_breakable_line = 'str' + 'str' + 'str' + 'str' + 'str' + 'str' + 'str' + 'str' + 'str' + 'str' + 'str' + 'str' + 'str' + 'str' + 'str' + 'str' + 'str' + 'str' + 'str' + 'str'

$ cat test2.py | python3 -m autopep8 -
mystr = 'abc'
path = f'aVeryLargeFStringLineThatBreak{mystr}aVeryLargeFStringLineThatBreaksWithaPep8'
long_breakable_line = 'str' + 'str' + 'str' + 'str' + 'str' + 'str' + 'str' + 'str' + \
    'str' + 'str' + 'str' + 'str' + 'str' + 'str' + \
    'str' + 'str' + 'str' + 'str' + 'str' + 'str'

@afarah1 afarah1 reopened this Dec 13, 2023
@karthiknadig
Copy link
Member

This is autopep8 issue, here is the output from autopep8 that the extension sees:
image

This is what we return back to the editor. As you can see there is a \n in the result from autopep8. Note that autopep8 seems to have different results based on python versions and versions of packages it depends on. So, when you ran it from the terminal it might have used a version where it worked. In my case it repro-ed on python 3.12. which is where most people are seeing the issue.

@karthiknadig karthiknadig closed this as not planned Won't fix, can't repro, duplicate, stale Dec 13, 2023
@github-actions github-actions bot added the info-needed Issue requires more information from poster label Dec 13, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 13, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
info-needed Issue requires more information from poster triage-needed Needs assignment to the proper sub-team
Projects
None yet
Development

No branches or pull requests

2 participants