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

Sort Imports command on VSCode not following PEP8 strictly #1731

Closed
everspader opened this issue May 13, 2021 · 3 comments
Closed

Sort Imports command on VSCode not following PEP8 strictly #1731

everspader opened this issue May 13, 2021 · 3 comments
Labels
question Further information is requested repo_needed Can't currently reproduce, if reproduction steps are added we will resivit this issue.

Comments

@everspader
Copy link

Environment data

  • VS Code version: 1.56.0
  • Extension version (available under the Extensions sidebar): v2021.5.829140558
  • OS and version: Ubuntu 20.04
  • Python version (& distribution if applicable, e.g. Anaconda): Python 3.8.5
  • Type of virtual environment used (N/A | venv | virtualenv | conda | ...): virtualenv
  • Value of the python.languageServer setting: pylance

Upon opening an issue on VSCode and another on vscode-python, I am opening an issue here as a last resort because I was told that vscode-python extension uses isort package for automatically sorting imports.

Expected behaviour

When using the Sort Import features in a script that contain standard libraries imports, third-party imports and local imports, I expect the output to conform with the imports section on PEP8, there should be a blank line between:

  1. Standard library imports.
  2. Related third-party imports.
  3. Local application/library-specific imports.

Therefore, the desired output would be:

import functools
import os

from django.db import models
from rest_framework import viewsets

from myapp.models import Upload

Actual behaviour

Let's say I write the following imports when creating a Django app.

import os
import functools
from rest_framework import viewsets
from django.db import models
from myapp.models import Upload

After I run the Sort Imports command, the output is:

import functools
import os

from django.db import models
from myapp.models import Upload
from rest_framework import viewsets

This means VSCode is sorting the third-party imports together with my local app imports alphabetically.

@gofr
Copy link
Contributor

gofr commented May 14, 2021

This sounds a lot like #1497. As I understand it, isort uses its working directory to determine what the local or first-party imports are. Everything else is third-party.

For VSCode I think the working directory will be the root of the workspace. If myapp isn't in the root of your workspace, isort doesn't find a corresponding file/directory and considers it to be third-party. You can add a config file to your project with the src_paths option to explicitly define paths to consider first-party.

@timothycrosley
Copy link
Member

timothycrosley commented May 15, 2021

Hi @everspader,

I'm sorry to hear you are experiencing this issue! I feel your pain, especially with unclearity about who to ask for help. On the upside I can say you have found the right place to report and discuss the issue. @gofr's info is spot on, and I hope you find it helpful! To fully understand why isort isn't able to determine what projects are first vs third party, I would need to know more about the structure of your project and what you think isort should be using to identify the import sections. Any chance you would have time over the next few weeks to look at it together over a video call, or otherwise, can you provide info about the general tree structure of your project, and what you think isort should recognize as first party?

Thanks!

~Timothy

@timothycrosley timothycrosley added question Further information is requested repo_needed Can't currently reproduce, if reproduction steps are added we will resivit this issue. labels May 15, 2021
@timothycrosley
Copy link
Member

Closing this as no additional information or repo has been provided. Please feel free to reopen, or create a new ticket if you are able to provide this in the future!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested repo_needed Can't currently reproduce, if reproduction steps are added we will resivit this issue.
Projects
None yet
Development

No branches or pull requests

3 participants