Skip to content

Auto import suggestions for collections.abc should take precedence over typing #1685

@DanielNoord

Description

@DanielNoord

Describe the Bug

When running pyrefly as LSP I'd expect imports that are available in collections.abc to take precedence of those coming from typing.
Considering they are deprecated, see for example https://docs.python.org/3/library/typing.html#typing.Iterable

For example in def func(x: Iterable[str]) -> None: ... the import from collections.abc import Iterable should come before from typing import Iterable.
However, pyrefly doesn't find it, or suggests to import it from _collections_abc.

I believe the logic for sorting these actions is in:

code_actions.sort_by(|(title1, _, _, _), (title2, _, _, _)| title1.cmp(title2));

That should probably learn about a certain precedence of certain modules over others.
However, at the moment pyrefly seems unable to even recognize collections.abc as a shim for _collections_abc.

I found the following code which could explain this:

ExportLocation::ThisModule(_) => vec![handle.dupe()],
// Re-exported modules like `foo` in `from from_module import foo`
// should likely be ignored in autoimport suggestions
// because the original export in from_module will show it.
// The current strategy will prevent intended re-exports from showing up in
// result list, but it's better than showing thousands of likely bad results.
ExportLocation::OtherModule(..) => Vec::new(),

I'd love to contribute to this project as I'd like to learn a bit more Rust and consider myself to have more than adequate knowledge of Python, but this seems like it might be too involved for a first contribution. If somebody is willing to coach me, I'd be up for trying that though!

Sandbox Link

I don't think the Sandbox supports generating samples of LSP output

(Only applicable for extension issues) IDE Information

No response

Metadata

Metadata

Assignees

Labels

language-serverIssues specific to our IDE integration rather than type checking

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions