Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyrefly/lib/commands/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ impl InferArgs {
let imports: Vec<(TextSize, String, String)> = transaction
.search_exports_exact(unknown_name)
.into_iter()
.map(|handle_to_import_from| {
.map(|(handle_to_import_from, _)| {
insert_import_edit_with_forced_import_format(
&ast,
handle.dupe(),
Expand Down
31 changes: 24 additions & 7 deletions pyrefly/lib/state/lsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

use std::cmp::Ordering;
use std::cmp::Reverse;
use std::collections::BTreeMap;

Expand Down Expand Up @@ -1676,7 +1677,9 @@ impl<'a> Transaction<'a> {
let error_range = error.range();
if error_range.contains_range(range) {
let unknown_name = module_info.code_at(error_range);
for handle_to_import_from in self.search_exports_exact(unknown_name) {
for (handle_to_import_from, export) in
self.search_exports_exact(unknown_name)
{
let (position, insert_text, _) = insert_import_edit(
&ast,
self.config_finder(),
Expand All @@ -1686,7 +1689,12 @@ impl<'a> Transaction<'a> {
import_format,
);
let range = TextRange::at(position, TextSize::new(0));
let title = format!("Insert import: `{}`", insert_text.trim());
let mut title = format!("Insert import: `{}`", insert_text.trim());

if export.deprecation.is_some() {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can probably be done more efficiently 😅

title.push_str(" (deprecated)");
}

code_actions.push((title, module_info.dupe(), range, insert_text));
}

Expand All @@ -1709,7 +1717,16 @@ impl<'a> Transaction<'a> {
_ => {}
}
}
code_actions.sort_by(|(title1, _, _, _), (title2, _, _, _)| title1.cmp(title2));

// Sort code actions: non-deprecated first, then alphabetically
code_actions.sort_by(|(title1, _, _, _), (title2, _, _, _)| {
match (title1.contains("deprecated"), title2.contains("deprecated")) {
(true, false) => Ordering::Greater,
(false, true) => Ordering::Less,
_ => title1.cmp(title2),
}
});
Comment on lines +1722 to +1728
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also feels very non-idiomatic...


Some(code_actions)
}

Expand Down Expand Up @@ -2934,11 +2951,11 @@ impl<'a> Transaction<'a> {
(result, is_incomplete)
}

pub fn search_exports_exact(&self, name: &str) -> Vec<Handle> {
pub fn search_exports_exact(&self, name: &str) -> Vec<(Handle, Export)> {
self.search_exports(|handle, exports| {
if let Some(export) = exports.get(&Name::new(name)) {
match export {
ExportLocation::ThisModule(_) => vec![handle.dupe()],
if let Some(export_location) = exports.get(&Name::new(name)) {
match export_location {
ExportLocation::ThisModule(export) => vec![(handle.dupe(), export.clone())],
// 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.
Expand Down
84 changes: 84 additions & 0 deletions pyrefly/lib/test/lsp/code_actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,87 @@ my_export
report.trim()
);
}

#[test]
fn test_import_from_stdlib() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test is great, i'd like to keep it

the one concern would be that different python / typeshed versions change where TypeVar comes from, but since we bundled typeshed it should match on all systems. an integration test of this behavior (where we actually use system python) would be less ideal than what you did.

let report = get_batched_lsp_operations_report_allow_error(
&[("a", "TypeVar('T')\n# ^")],
get_test_report,
);
// TODO: Ideally `typing` would be preferred over `ast`.
assert_eq!(
r#"
# a.py
1 | TypeVar('T')
^
Code Actions Results:
# Title: Insert import: `from ast import TypeVar`

## Before:
TypeVar('T')
# ^
## After:
from ast import TypeVar
TypeVar('T')
# ^
# Title: Insert import: `from typing import TypeVar`

## Before:
TypeVar('T')
# ^
## After:
from typing import TypeVar
TypeVar('T')
# ^
"#
.trim(),
report.trim()
);
}

#[test]
fn test_take_deprecation_into_account_in_sorting_of_actions() {
let report = get_batched_lsp_operations_report_allow_error(
&[
(
"a",
"from warnings import deprecated\n@deprecated('')\ndef my_func(): pass",
),
("b", "def my_func(): pass"),
("c", "my_func()\n# ^"),
],
get_test_report,
);
assert_eq!(
r#"
# a.py

# b.py

# c.py
1 | my_func()
^
Code Actions Results:
# Title: Insert import: `from b import my_func`

## Before:
my_func()
# ^
## After:
from b import my_func
my_func()
# ^
# Title: Insert import: `from a import my_func` (deprecated)

## Before:
my_func()
# ^
## After:
from a import my_func
my_func()
# ^
"#
.trim(),
report.trim()
);
}