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

Add heuristics for add imports quick fixes #5226

Closed
luabud opened this issue Dec 7, 2023 · 10 comments
Closed

Add heuristics for add imports quick fixes #5226

luabud opened this issue Dec 7, 2023 · 10 comments
Assignees
Labels
enhancement New feature or request fixed in next version (main) A fix has been implemented and will appear in an upcoming version

Comments

@luabud
Copy link
Member

luabud commented Dec 7, 2023

Discussed offline with the broader team and we got to an agreement with only showing at most the top 3 high confidence add import options in the quick fix list, and then show an additional source action to "Search for an import match" that opens up the searchable quick pick menu with more import matches.

"High confidence" can be implemented as heuristics around:

  • Packages listed in requirements.txt, environment.yml files
  • Packages installed in the environment
  • Packages already imported in other files in the workspace
@github-actions github-actions bot added the needs repro Issue has not been reproduced yet label Dec 7, 2023
@Avasam
Copy link

Avasam commented Dec 7, 2023

Curious to see what comes out of this. I often get half-a-dozen unrelated autoimport suggestions when working in typeshed

@luabud luabud added enhancement New feature or request and removed needs repro Issue has not been reproduced yet labels Dec 12, 2023
@StellaHuang95 StellaHuang95 removed their assignment Dec 14, 2023
@heejaechang
Copy link
Contributor

related issue - https://github.com/microsoft/pyrx/issues/4321

@luabud
Copy link
Member Author

luabud commented Jan 31, 2024

@heejaechang I think it makes sense to tackle #5153 along with this (i.e. to remove the exact match setting). What do you think?

@heejaechang
Copy link
Contributor

heejaechang commented Feb 2, 2024

Here is my preliminary plan for enhancing the add using feature.

  1. Limit the total number of add import suggestions in code action to 3.
  2. Criteria for selecting the 3 are:
    a. Use MRU (Most Recently Used) for exact matches (up to 3).
    b. If slots remain, prefer the same module used in other files for the same symbol.
    c. If slots remain, prioritize one from typing or stdlib, with possible user configuration.
    d. if slots remain, choose from user modules.
    e. If slots remain, choose from third-party packages.
    f. Otherwise, sort by module and symbol name.
    [g. If no exact match exists, include up to 3 fuzzy matched imports using the same criteria (if searching word has more than 3 chars) -- this has been cut. it will be only available in search import]
  3. Track around 100 MRUs (100 is an arbitrary number).
  4. Introduce a new search import in code action (marked as refactoring/low priority) that opens a quick pick window, allowing users to search for and add imports
    a. e.g., searching model to find the correct django models when the exact name is forgotten or
    b. find the correct import name to insert if the original type name differs from the package's re-export. For example, search for WebDriver but insert ChromiumEdge from selenium since WebDriver is an internal type name.

Goals:

  1. Show a limited number of the most probable options for add imports in code actions.
  2. Provide a way for users to find an import even without remembering the precise name of the import/symbol.

@heejaechang
Copy link
Contributor

heejaechang commented Feb 2, 2024

I think it makes sense to tackle #5153 along with this (i.e. to remove the exact match setting). What do you think?

sure, we probably will remove it once everything is done. but we probably will do a/b testing and feature rollout and etc? so, existing stuff will stay until we are done with all those?

@luabud
Copy link
Member Author

luabud commented Feb 3, 2024

I really like the proposal, thanks for working on that! My one suggestion would be to not do g.
i.e. If there aren't exact matches we wouldn't show any import suggestions, only the code action for "Search for an import match". That's just my vote though, ofc :)

@heejaechang
Copy link
Contributor

I really like the proposal, thanks for working on that! My one suggestion would be to not do g.
i.e. If there aren't exact matches we wouldn't show any import suggestions, only the code action for "Search for an import match". That's just my vote though, ofc :)

so we don't even do fuzzy matching for misspelling? currently we have that since pyright's original implementation has it.

@luabud
Copy link
Member Author

luabud commented Feb 5, 2024

I was thinking we would in the "show more options" list, but not as top level code actions

@PylanceBot PylanceBot added the fixed in next version (main) A fix has been implemented and will appear in an upcoming version label Feb 13, 2024
@bschnurr
Copy link
Member

This issue has been fixed in prerelease version 2024.2.100, which we've just released. You can find the changelog here: CHANGELOG.md

@rchiodo
Copy link
Contributor

rchiodo commented Feb 15, 2024

In order to enable this new enhancement, add the following to your settings.json:

    "python.analysis.addImport.heuristics": true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request fixed in next version (main) A fix has been implemented and will appear in an upcoming version
Projects
None yet
Development

No branches or pull requests

7 participants