Vscode-python: Refactor: sort imports fails with "Invalid patch string: import XXX"

Created on 9 Sep 2020  路  3Comments  路  Source: microsoft/vscode-python

Environment data

  • VS Code version: 1.48.2
  • Extension version (available under the Extensions sidebar): 2020.8
  • OS and version: Linux Ubuntu 20.04 x86_64
  • Python version (& distribution if applicable, e.g. Anaconda): 3.8.2
  • Type of virtual environment used (N/A | venv | virtualenv | conda | ...): venv
  • Relevant/affected Python packages and their versions: isort 5.5.1
  • Relevant/affected Python-related VS Code extensions and their versions: XXX
  • Value of the python.languageServer setting: Pylance

Expected behaviour

Invoking "Refactor: sort imports" runs isort and sorts import declarations in current document

Actual behaviour

Zrzut ekranu z 2020-09-09 20-46-13

Steps to reproduce:

  1. install isort in venv
  2. configure workspace to use custom isort
  3. open Python file
  4. invoke "Refactor: sort imports" from command palette

Logs

Output:

Error: Invalid patch string: import ast
Error 2020-09-09 20:35:15: Failed to format imports for '/home/jazg/work/bbbrating/server/setup.py'. [Error: Invalid patch string: import ast
    at n.y (/home/jazg/.vscode/extensions/ms-python.python-2020.8.106424/out/client/extension.js:1:515081)
    at v.getWorkspaceEditsFromPatch (/home/jazg/.vscode/extensions/ms-python.python-2020.8.106424/out/client/extension.js:1:518006)
    at b._provideDocumentSortImportsEdits (/home/jazg/.vscode/extensions/ms-python.python-2020.8.106424/out/client/extension.js:56:895891)
    at processTicksAndRejections (internal/process/task_queues.js:85:5)]

File setup.py begins with

import ast
import codecs

This generally fails on 1st import statement (other imports in other modules).

isort itself works OK and does not produce any error on this file.

type-bug

All 3 comments

This broke with isort 5.5, works fine with 5.4

@zgoda, The issue was because of https://github.com/PyCQA/isort/issues/1469. It's solved in isort 5.5.2.

Thanks.

Was this page helpful?
0 / 5 - 0 ratings