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

Relative imports broken with "python.analysis.extraPaths" #3194

Closed
danvilar opened this issue Aug 13, 2022 · 6 comments
Closed

Relative imports broken with "python.analysis.extraPaths" #3194

danvilar opened this issue Aug 13, 2022 · 6 comments
Assignees
Labels
bug Something isn't working fixed in next version (main) A fix has been implemented and will appear in an upcoming version

Comments

@danvilar
Copy link

Environment data

  • Language Server version: v2022.8.20
  • OS and version: Arch Linux 5.18.16-arch1-1
  • Python version (& distribution if applicable, e.g. Anaconda): Python 3.10.5 (provided by pyenv)

Repro Steps

  1. Add a path to "python.analysis.extraPaths". In my case, since I'm using PDM:
...
"python.analysis.extraPaths": [
        "__pypackages__/3.10/lib"
    ],
...
  1. Change the "python.analysis.importFormat" from "absolute" to "relative":
...
"python.analysis.importFormat": "relative",
...

Expected behavior

When saving a python file, keep relative imports that are related to the package, and keep the imports from packages that are installed inside the "pypackages/3.10/lib" as normal inputs; not adding a relative path.

Example:

from fastapi import FastAPI

from fastapi.responses import HTMLResponse

from .database import SessionLocal
from .database.schemas import SearchQuery
from loguru import logger
from itertools import chain

...

Actual behavior

The imports that are related to packages installed inside "pypackages/3.10/lib" are reformated to be a relative import.

Example:

from ..__pypackages__.3.10.lib.fastapi import FastAPI

from ..__pypackages__.3.10.lib.fastapi.responses import HTMLResponse

from .database import SessionLocal
from .database.schemas import SearchQuery
from ..__pypackages__.3.10.lib.loguru import logger
from itertools import chain

...

Logs

XXX
@judej judej added the needs investigation Could be an issue - needs investigation label Aug 15, 2022
@heejaechang heejaechang self-assigned this Aug 31, 2022
@heejaechang heejaechang added bug Something isn't working fixed in next version (main) A fix has been implemented and will appear in an upcoming version and removed needs investigation Could be an issue - needs investigation labels Aug 31, 2022
@debonte
Copy link
Contributor

debonte commented Sep 8, 2022

This issue has been fixed in prerelease version 2022.9.11, which we've just released. You can find the changelog here: CHANGELOG.md

@debonte debonte closed this as completed Sep 8, 2022
@mh-firouzjah
Copy link

after updating to 1.71.2 still I have the same issue
if I set "python.analysis.importFormat": "relative", then it will work as @danvilar said, and if I don't set anything, the vscode will change all relative imports in python file to absolute path, e.g. converts from .models import model_name to from root.app.models import model_name but this behavior will cause some errors at run time. why the vscode would change these import statements at all? I thought it is the behavior of isort which I used to be using via "python.sortImports.args" but now a days I've seen ms-python has a new extension isort and I've installed it but again this plugin seems to have nothing to do with the code and python extension still using the old settings(disabling the isort extension didn't change anything).

@mh-firouzjah
Copy link

why the vscode would change these import statements at all?

it seems by setting as following(either for python language or the global vscode behavior), vscode will stop changing the relative imports to full path imports.

"editor.codeActionsOnSave": {
    "source.fixAll": false,
  },

@heejaechang
Copy link
Contributor

"source.fixAll": true basically run every source.fixAll code action registered by extension. and that is probably why our new fix all got called.

if you put "false" for all our fix all code actions, it shouldn't try to fix
ex)
"source.fixAll.unusedImports": false,
"source.fixAll.convertImportFormat": false

see this for all our fix all code actions. https://github.com/microsoft/pylance-release#source-code-actions

@heejaechang
Copy link
Contributor

see this comment for more detail - #3181 (comment)

vscode team has a tracking issue for better "editor.codeActiosOnSave": microsoft/vscode#82718

@mh-firouzjah
Copy link

@heejaechang thank you for your reply that was very helpful to know that "source.fixAll.convertImportFormat": false is possible, but I wan to mention that when I change my vscode settings usually to see if there is a setting available I will type part of that and then by pressing ctrl+space the suggestion menu will help to figure out if anything exists or not. but for source.fixAll. by typing the dot at the end there is no such suggestion so I didn't see that it is possible to have more option there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed in next version (main) A fix has been implemented and will appear in an upcoming version
Projects
None yet
Development

No branches or pull requests

5 participants