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

Pylance fails to import just few classes #6209

Open
besil opened this issue Jul 30, 2024 · 7 comments
Open

Pylance fails to import just few classes #6209

besil opened this issue Jul 30, 2024 · 7 comments
Assignees
Labels
enhancement New feature or request needs repro Issue has not been reproduced yet

Comments

@besil
Copy link

besil commented Jul 30, 2024

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,
}
@github-actions github-actions bot added the needs repro Issue has not been reproduced yet label Jul 30, 2024
@besil
Copy link
Author

besil commented Jul 30, 2024

Related SO question opened by me

@heejaechang
Copy link
Contributor

heejaechang commented Jul 30, 2024

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
Copy link
Author

besil commented Jul 30, 2024

I tried, but issue persists

@heejaechang
Copy link
Contributor

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.

Screen.Recording.2024-07-30.122727.mp4

let's improve our logic so, we only dedup between different packages within stdlib

@heejaechang heejaechang added the bug Something isn't working label Jul 30, 2024
@heejaechang
Copy link
Contributor

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
Copy link
Author

besil commented Jul 31, 2024

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

@jscheel
Copy link

jscheel commented Nov 8, 2024

I am experiencing this with pydantic.

In some envs I get this:
Image

And in others (that have other packages installed), I get this:
Image

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request needs repro Issue has not been reproduced yet
Projects
None yet
Development

No branches or pull requests

4 participants