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

Import suggestions ignores imports made on package level #5485

Closed
anderssonjohan opened this issue Feb 19, 2024 · 5 comments
Closed

Import suggestions ignores imports made on package level #5485

anderssonjohan opened this issue Feb 19, 2024 · 5 comments
Assignees
Labels
waiting for user response Requires more information from user

Comments

@anderssonjohan
Copy link

Import suggestions never lists package level declarations, only module level.

Problem: I want users of my package to import symbols from package level and not module level. VSCode suggests importing symbols from package.module instead of package.

Environment data

  • Language Server version: 2024.2.2 (pyright version 1.1.348, commit cfb1de0c)
  • OS and version: macOS 12.7.3
  • Python version (& distribution if applicable, e.g. Anaconda): 3.11.1

FWIW, I'm NOT using the exact match setting when testing this:

  "python.analysis.addImport.exactMatchOnly": true

Code Snippet

The following code is not being shown in the list of import suggestions:

pylance_imports_test/__init__.py

from pylance_imports_test.my_module import MyClass

Module defined as:

pylance_imports_test/my_module.py

class MyClass:
    pass

Repro Steps

  1. Define a class in a module that is defined on the package level using a from module import ... import
  2. Write some code using the exported class
  3. Engage the import suggester
  4. VSCode does not include the package level export as a suggestion

Expected behavior

Screenshot 2024-02-19 at 12 40 10

Note: This behavior was produced by modifying the __init__.py from using a from .... import .... to use variables:

import pylance_imports_test.my_module

MyClass = pylance_imports_test.my_module.MyClass

Actual behavior

Screenshot 2024-02-19 at 12 36 46

Logs

The logs prints [found 0], unless the workaround of declaring an explicit variable is used. Then it prints [found 1]:

2024-02-19 12:48:54.288 [info] [Info  - 12:48:54] (15502) SourceFile: Received fs event 'change' for path 'pylance_imports_test/__init__.py'
2024-02-19 12:48:54.312 [info] (15502) [BG(1)] indexing: file:///pylance_imports_test/__init__.py [found 1] (0ms)
2024-02-19 12:48:54.313 [info] (15502) Indexing Done: file:///pylance_imports_test/__init__.py
@github-actions github-actions bot added the needs repro Issue has not been reproduced yet label Feb 19, 2024
@heejaechang
Copy link
Contributor

that's behavior for user files, when it is installed as third party packages (installed in site-packages), you will get the behavior you want.

@heejaechang heejaechang added waiting for user response Requires more information from user and removed needs repro Issue has not been reproduced yet labels Feb 20, 2024
@anderssonjohan
Copy link
Author

anderssonjohan commented Feb 23, 2024

that's behavior for user files, when it is installed as third party packages (installed in site-packages), you will get the behavior you want.

@heejaechang Ok, thanks for the quick reply and explanation. I get what you mean but packages installed in the venv's site-packages are never suggested (only the message "No code actions available")

EDIT: Example of a package installed in site-packages of the current venv.

It's not an issue finding the imports when I type them by hand, but the packages in the venvs site-packages are never suggested using alt-Enter.

Screenshot 2024-02-23 at 17 12 37

Typing the import works and types are discovered:
Screenshot 2024-02-23 at 17 10 53

(I'm not using any extraPaths settings)

@github-actions github-actions bot added user responded Was "waiting for user response" and they responded and removed waiting for user response Requires more information from user labels Feb 23, 2024
@heejaechang
Copy link
Contributor

heejaechang commented Mar 7, 2024

That's because, by default, pylance will try not to spend too much time on indexing. Indexing full packages installed is expensive, and not everyone wants or uses those expensive features. In other words, they don't want to spend 10 minutes waiting for VSCode to finish indexing their workspace/repo.

So, we have this option python.analysis.packageIndexDepths that allows users to let us spend many minutes to index. For example, if you add this:

{
  "name": "lime_webhooks",
  "depth": 5,
  "includeAllSymbols": true
}

I believe you will get what you want. For more detail on the option, see the description.

@heejaechang heejaechang added waiting for user response Requires more information from user and removed user responded Was "waiting for user response" and they responded labels Mar 7, 2024
@heejaechang
Copy link
Contributor

heejaechang commented Mar 7, 2024

we also have this issue - #5434 - to address root issue (providing mode (full) that allowes pylance to consume a lot more resources to provide better user experiences)

Copy link
Contributor

github-actions bot commented Apr 7, 2024

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.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Apr 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
waiting for user response Requires more information from user
Projects
None yet
Development

No branches or pull requests

2 participants