pylance-release
pylance-release copied to clipboard
Pylance fails to import just few classes
Hi guys, thank you for your work on Pylance.
I'm trying VSCode for Python development to move away from PyCharm, but I'm having a strange issue and I don't know if it's a bug or not.
So far, I'm able to get import suggestions like this:

and selecting the class will import it correctly.
But, if I try with other django core views, such as ListView or LoginRequiredMixin, this happens:


where django_tables2.views and hijack.views are third party libraries, which import themselves the django.views.generic.ListView class.
As you can imagine, ListView and UpdateView are in the same Django package (django.views.generic). They are in 2 different python files (update.py and list.py respectively), but they are both exported in __init__.py at package level.
I already customized python.analysis.packageIndexDepths as suggested in other issues, but it's quite strange to me that UpdateView is resolved and ListView not.
I can successfully import other classes in the same django.views.generic.list.py file, as expected.
Is this a bug or I'm just missing something very basic?
Please find my full settings.json:
{
"python.testing.pytestArgs": [
"."
],
"python.testing.unittestEnabled": false,
"python.languageServer": "Pylance",
"python.testing.pytestEnabled": true,
"python.analysis.autoFormatStrings": true,
"python.analysis.autoSearchPaths": false,
"python.analysis.autoImportCompletions": true,
"python.analysis.inlayHints.callArgumentNames": "off",
"python.analysis.inlayHints.functionReturnTypes": true,
"python.analysis.userFileIndexingLimit": -1,
"python.analysis.indexing": true,
"python.analysis.stubPath": "",
"python.analysis.inlayHints.variableTypes": true,
"python.analysis.typeCheckingMode": "off",
"python.autoComplete.addBrackets": true,
"python.analysis.completeFunctionParens": true,
"python.analysis.packageIndexDepths": [
{
"name": "",
"depth": 10,
"includeAllSymbols": true
},
],
"python.experiments.enabled": false,
}
Related SO question opened by me
can you run clear all persisted indices command and reload window and see whether that fixed the issue? oh, and don't forget to wait until initial indexing is done. you can see it from output - python language server
I tried, but issue persists
I think what is happening is when we dedup import symbols, we think one from third party is better since it has shorter import path. without those third party lib installed, I get what you want.
https://github.com/user-attachments/assets/94fc5345-6154-4c2f-913d-91785518fb84
let's improve our logic so, we only dedup between different packages within stdlib
I think the root issue is this setting that basically says just blindly include all symbols to the indices, rather you could configure it to be more specific
"python.analysis.packageIndexDepths": [
{
"name": "",
"depth": 10,
"includeAllSymbols": true
},
],
for example, if you add
{
"name": "django_tables2",
"depth": 10,
"includeAllSymbols": false
},
ListView in django_tables2 won't be picked up. same for the hijack. basically that includeAllSymbols: true cause all imported symbols from other packages are included as well.
...
that said, I think we can improve user experience on this.
Thank you @heejaechang
Changing my settings.json from
...
"python.analysis.packageIndexDepths": [
{
"name": "",
"depth": 10,
"includeAllSymbols": true
}
],
...
to
...
"python.analysis.packageIndexDepths": [
{
"name": "",
"depth": 10,
"includeAllSymbols": false
}
],
...
removed any 3rd party duplicated import and correctly references the right library.
I am experiencing this with pydantic.
In some envs I get this:
And in others (that have other packages installed), I get this:
Notice that sometimes I get it suggesting from pydantic.v1 import BaseModel, and other times I get it appropriately suggesting from pydantic import BaseModel.
...
"python.analysis.packageIndexDepths": [
{
"name": "",
"depth": 10,
"includeAllSymbols": false
}
],
...
does not seem to help in this instance.
This seems to be happening for some exports from pydantic but not others, even ones defined in the same file. For example RootModel will properly suggest pydantic, but Field will suggest pydantic.v1.