pylance-release
pylance-release copied to clipboard
Language server suggests auto-importing stdlib functions from local project files instead
Describe the bug
The language server suggest importing functions from files that import them rather than the declaration module. This makes sense for re-exports (e.g. from ... import x as x), but not otherwise.
Code or Screenshots Create a simple python module + file with an import:
from os import abort
In a new file try to get the autoimport for abort:
At this point, the language server will suggest two imports of abort - one from typing (correct) and one from the file created earlier (incorrect). This gets worse the more you import that specific function in your project, suggesting import from each file that uses it.
Additionally, you can't tell which is which (as you can see above), as the only hint is Auto-import. It would be nice if the hint were replaced with the package name, would be very helpful for imports from typing vs typing_extensions, for example.
Information
Using PyRight 1.1.374 with the built-in language server (i.e. from .venv) in PyCharm.
Could someone from the pylance team please transfer this to the pylance-release project and triage/prioritize it as you see fit? Thanks!
Additionally, you can't tell which is which (as you can see above), as the only hint is Auto-import. It would be nice if the hint were replaced with the package name,
We are already returning this info in our textDocument/completion LSP response as the labelDetails.description property. My guess is that PyCharm doesn't support this yet -- it was added in LSP 3.17. I'd recommend filing an enhacement request on PyCharm.
Here's a snippet from the LSP response that I saw when following your repro steps using Pyright 1.1.374 in VS Code:
{
"label": "abort",
"kind": 3,
"data": {
"uri": "file:///c%3A/issues/pylance/6219/test2.py",
"position": {
"line": 0,
"character": 5
},
"autoImportText": "```\nfrom mylib import abort\n```",
"symbolLabel": "abort"
},
"sortText": "12.9999.abort.05.mylib",
"detail": "Auto-import",
"labelDetails": {
"description": "mylib"
},
And here's what VS Code shows:
@debonte I understand, thanks for the clarification, but that's not the issue. The hint thing was just an addition, hence the "additionally". The problem is that is suggests the same import multiple times. It should not suggest importing a standard library function from my own project files, as they could stop importing it at any time. Can this be reopened?
Can you provide more details on the files/directories involved in your repro scenario?
isn't this dup of this? https://github.com/microsoft/pylance-release/issues/6221
The problem is that is suggests the same import multiple times.
For this, I dont think it is the same import. I believe it is symbols with the same name from multiple modules.
It should not suggest importing a standard library function from my own project files, as they could stop importing it at any time.
I believe this is due to pyright doesn't do deduping but still suggesting alias symbol from user files so, if user has
a.py
from os import abort
pyright will suggest abort from both os (original module) and a (imported symbol)
this shouldn't happen for pylance since we do deduping for third party libraries symbols and don't suggest alias symbols from user files
that said, for pyright, we could do deduping (with perf cost since we need to do it every time we show completion) or stop suggesting import alias from user files
This issue has been closed automatically because it needs more information and has not had recent activity. If the issue still persists, please reopen with the information requested. Thanks.