microsoft / pylance-release

Documentation and issues for Pylance
Creative Commons Attribution 4.0 International
1.7k stars 768 forks source link

Pylance fails to import just few classes #6209

Open besil opened 1 month ago

besil commented 1 month ago

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,
}
besil commented 1 month ago

Related SO question opened by me

heejaechang commented 1 month ago

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

besil commented 1 month ago

I tried, but issue persists

heejaechang commented 1 month ago

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

heejaechang commented 1 month ago

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.

besil commented 1 month ago

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. image