microsoft/vscode-autopep8

Auto-formatting inside f-strings

amirstm opened this issue · 4 comments

Hey.
The latest version of the extension has a problem that edits inside python f-strings. For instance:
Line of code before formatting:
connector = f"socks5://{user}:{password}:{url}:{port}"
Line after saving with the formatter enabled:
connector = f"socks5: //{user}: {password}: {url}: {port}"

@amirstm I am not able to repro this locally. Can you run this from the command line and see if this is actually coming from autopep8?
image

from the logs it looks like it is processing it as expected:

2023-10-16 08:28:44.746 [info] [Trace - 8:28:44 AM] Sending notification '$/setTrace'.
2023-10-16 08:28:44.746 [info] Params: {
    "value": "verbose"
}


2023-10-16 08:28:54.762 [info] [Trace - 8:28:54 AM] Sending request 'textDocument/formatting - (1)'.
2023-10-16 08:28:54.762 [info] Params: {
    "textDocument": {
        "uri": "file:///c%3A/GIT/formatters/vscode-autopep8/bundled/tool/something.py"
    },
    "options": {
        "tabSize": 4,
        "insertSpaces": true
    }
}


2023-10-16 08:28:54.779 [info] [Trace - 8:28:54 AM] Received notification 'window/logMessage'.
2023-10-16 08:28:54.780 [info] Params: {
    "type": 4,
    "message": "c:\\GIT\\formatters\\vscode-autopep8\\.venv\\Scripts\\python.exe -m autopep8 -"
}


2023-10-16 08:28:54.780 [info] c:\GIT\formatters\vscode-autopep8\.venv\Scripts\python.exe -m autopep8 -
2023-10-16 08:28:54.781 [info] [Trace - 8:28:54 AM] Received notification 'window/logMessage'.
2023-10-16 08:28:54.781 [info] Params: {
    "type": 4,
    "message": "CWD Server: c:\\GIT\\formatters\\vscode-autopep8"
}


2023-10-16 08:28:54.781 [info] CWD Server: c:\GIT\formatters\vscode-autopep8
2023-10-16 08:28:55.930 [info] [Trace - 8:28:55 AM] Received notification 'window/logMessage'.
2023-10-16 08:28:55.930 [info] Params: {
    "type": 4,
    "message": "file:///c%3A/GIT/formatters/vscode-autopep8/bundled/tool/something.py :\r\n****************************************************************************************************\r\nconnector = f\"socks5://{user}:{password}:{url}:{port}\"\r\n****************************************************************************************************\r\n"
}


2023-10-16 08:28:55.930 [info] file:///c%3A/GIT/formatters/vscode-autopep8/bundled/tool/something.py :
****************************************************************************************************
connector = f"socks5://{user}:{password}:{url}:{port}"
****************************************************************************************************

image

You can get detailed logs by adding the following to your user settings:
image

@karthiknadig Thank you for your response. I ran the following command from the command line and it returned the same output.
image
the logs look like this:

2023-10-16 20:21:30.112 [info] [Trace - 8:21:30 PM] Sending request 'textDocument/formatting - (1)'.
2023-10-16 20:21:30.112 [info] Params: {
    "textDocument": {
        "uri": "file:///home/amirs/test/test.py"
    },
    "options": {
        "tabSize": 4,
        "insertSpaces": true
    }
}


2023-10-16 20:21:30.116 [info] [Trace - 8:21:30 PM] Received notification 'window/logMessage'.
2023-10-16 20:21:30.116 [info] Params: {
    "type": 4,
    "message": "/bin/python -m autopep8 -"
}


2023-10-16 20:21:30.116 [info] /bin/python -m autopep8 -
2023-10-16 20:21:30.116 [info] [Trace - 8:21:30 PM] Received notification 'window/logMessage'.
2023-10-16 20:21:30.116 [info] Params: {
    "type": 4,
    "message": "CWD Server: /home/amirs/test"
}


2023-10-16 20:21:30.116 [info] CWD Server: /home/amirs/test
2023-10-16 20:21:30.159 [info] [Trace - 8:21:30 PM] Received notification 'window/logMessage'.
2023-10-16 20:21:30.159 [info] Params: {
    "type": 4,
    "message": "file:///home/amirs/test/test.py :\r\n****************************************************************************************************\r\nconnector = f\"socks5: //{user}: {password}: {url}: {port}\"\n\r\n****************************************************************************************************\r\n"
}


2023-10-16 20:21:30.159 [info] file:///home/amirs/test/test.py :
****************************************************************************************************
connector = f"socks5: //{user}: {password}: {url}: {port}"

****************************************************************************************************

2023-10-16 20:21:30.160 [info] [Trace - 8:21:30 PM] Received response 'textDocument/formatting - (1)' in 48ms.
2023-10-16 20:21:30.160 [info] Result: [
    {
        "range": {
            "start": {
                "line": 0,
                "character": 0
            },
            "end": {
                "line": 1,
                "character": 0
            }
        },
        "newText": "connector = f\"socks5: //{user}: {password}: {url}: {port}\"\n"
    }
]


2023-10-16 20:21:30.206 [info] [Trace - 8:21:30 PM] Sending notification 'textDocument/didChange'.
2023-10-16 20:21:30.206 [info] Params: {
    "textDocument": {
        "uri": "file:///home/amirs/test/test.py",
        "version": 8
    },
    "contentChanges": [
        {
            "range": {
                "start": {
                    "line": 0,
                    "character": 47
                },
                "end": {
                    "line": 0,
                    "character": 47
                }
            },
            "rangeLength": 0,
            "text": " "
        },
        {
            "range": {
                "start": {
                    "line": 0,
                    "character": 41
                },
                "end": {
                    "line": 0,
                    "character": 41
                }
            },
            "rangeLength": 0,
            "text": " "
        },
        {
            "range": {
                "start": {
                    "line": 0,
                    "character": 30
                },
                "end": {
                    "line": 0,
                    "character": 30
                }
            },
            "rangeLength": 0,
            "text": " "
        },
        {
            "range": {
                "start": {
                    "line": 0,
                    "character": 21
                },
                "end": {
                    "line": 0,
                    "character": 21
                }
            },
            "rangeLength": 0,
            "text": " "
        }
    ]
}

I should point out that I am using python3.12, and when I tried the same with python3.11 the issue didn't exist.

@amirstm This is a bug on autopep8 itself. So, please file this on https://github.com/hhatto/autopep8/issues

Closing this against the upstream issue hhatto/autopep8#712