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

Recommended way to invoke isort programatically #1940

Open
karthiknadig opened this issue Jun 7, 2022 · 0 comments
Open

Recommended way to invoke isort programatically #1940

karthiknadig opened this issue Jun 7, 2022 · 0 comments

Comments

@karthiknadig
Copy link

I work on the vscode-python extension, we have a isort extension based on LSP as mentioned here #1904.

We have few users complaining that the behavior they see with isort is different from what they expect. We don't really pass anything different between how we used to do it in vscode-python vs vscode-isort extensions. The only main difference is how they are invoked.

In vscode-python extension, we insert the path to isort bundled with the python extension and then call main on it.

isort_path = os.path.join(os.path.dirname(__file__), "lib", "python")
sys.path.insert(0, isort_path)

import isort.main

isort.main.main()

in vscode-python extension we get the diff and try to calculate the edits. The content of the file is passed in via stdin.

isort - --diff

# with --profile black
isort - --diff --profile black

Where as in vscode-isort, we insert the path to isort bundled with the python extension and then call runpy.run_module on it.

sys.path.append(str(pathlib.Path(__file__).parent.parent / "libs"))
result = utils.run_module(
                module=module, argv=argv, use_stdin=True, cwd=cwd, source=source
            )

in vscode-isort extension we get the formatted content and use it directly. The content of the file is passed in via stdin.

isort - --filename <path>

# with --profile black
isort - --filename <path> --profile black

So not really sure why there is any difference here. Full discussion here: microsoft/vscode-isort#53

Please let me know if there is a better API to call in a server like scenario.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant