Skip to content

Conversation

@vinismarques
Copy link

@vinismarques vinismarques commented Nov 29, 2025

Summary

This PR fixes #1704 by filtering out implicitly re-exported builtins from import suggestions and improves the robustness of completions when trailing whitespace is present.

Specific changes:

  • Filter Builtin Exports: Filters out exports that originate from builtins or extra_builtins (e.g., str, Exception) when providing completion candidates for a module. This prevents polluting the completion list with global builtins that aren't explicitly exported by the target module. Standard module attributes (like __annotations__, __doc__) remain available.
  • Trailing Whitespace Support: Adds logic to handle import and from ... import statements ending in trailing spaces. Previously, the parser yielded no nodes for these cursor positions, preventing completions; now, we detect the context and provide appropriate suggestions (e.g., showing the module list after import <cursor>).
  • Refactoring: Extracted completion logic into add_module_exports_completions and add_all_modules_completions to reduce code duplication and simplify the main completion function.
  • Docs: Fixed a minor typo in CONTRIBUTING.md.

Fixes #1704

Test Plan

Ran python3 test.py to verify the following scenarios:

  • Builtin Filtering: Verified that importing from a module does not suggest builtin types like str or int, but still suggests valid exports and module attributes (verified via test_completion_from_import_builtins).
  • Trailing Spaces: Verified that completions appear correctly when the cursor is separated from the import keyword by spaces (verified via test_completion_import_trailing_space and test_completion_from_import_trailing_space).
  • Conformance: All existing conformance tests passed.

Filter out exports from builtins and extra_builtins in LSP import completion.

This prevents suggesting objects that are not actually present in the module when importing from another module.

Added a test case to verify that builtins are not suggested in import completions.
- Refactored completion logic in lsp.rs to handle cases where the parser returns no nodes (e.g. cursor after 'import ' or 'from ... import ' with trailing space).
- Introduced add_module_exports_completions and add_all_modules_completions helpers.
- Updated existing regression tests in completion.rs that were previously asserting no completions for these scenarios.
- Added new tests covering single and multiple trailing spaces for both 'import' and 'from ... import' statements.
- Collapse nested if statements in 'add_module_exports_completions' and 'completion'
- Replace 'to_string()' with 'to_owned()' for 'root_module_str' to fix clippy error
@meta-cla meta-cla bot added the cla signed label Nov 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Wrong import suggestions

1 participant