Skip to content

Commit fbefb5d

Browse files
committed
Softens unused import diagnostic to "may be unused" instead of "is unused", per #3949
Fixes #3949 Documentation for IDE-only unused-import hint added to IDE-features.mdx Avoided error-kinds.mdx because it's only an IDE hint
1 parent 887d0f3 commit fbefb5d

4 files changed

Lines changed: 14 additions & 11 deletions

File tree

pyrefly/lib/lsp/non_wasm/server.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5370,7 +5370,7 @@ impl Server {
53705370
range: lsp_range,
53715371
severity: Some(DiagnosticSeverity::HINT),
53725372
source: Some("Pyrefly".to_owned()),
5373-
message: format!("Import `{}` is unused", unused.name.as_str()).into(),
5373+
message: format!("Import `{}` may be unused", unused.name.as_str()).into(),
53745374
code: Some(NumberOrString::String("unused-import".to_owned())),
53755375
code_description: None,
53765376
related_information: None,

pyrefly/lib/test/lsp/diagnostic.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ fn get_unused_import_diagnostics(state: &State, handle: &Handle) -> String {
2323
if i > 0 {
2424
report.push_str(", ");
2525
}
26-
report.push_str(&format!("Import `{}` is unused", unused.name.as_str()));
26+
report.push_str(&format!("Import `{}` may be unused", unused.name.as_str()));
2727
}
2828
report
2929
} else {
@@ -76,7 +76,7 @@ def foo() -> str:
7676
let (handles, state) = mk_multi_file_state(&[("main", code)], Require::Exports, true);
7777
let handle = handles.get("main").unwrap();
7878
let report = get_unused_import_diagnostics(&state, handle);
79-
assert_eq!(report, "Import `os` is unused");
79+
assert_eq!(report, "Import `os` may be unused");
8080
}
8181

8282
#[test]
@@ -104,7 +104,7 @@ def foo() -> str:
104104
let (handles, state) = mk_multi_file_state(&[("main", code)], Require::Exports, true);
105105
let handle = handles.get("main").unwrap();
106106
let report = get_unused_import_diagnostics(&state, handle);
107-
assert_eq!(report, "Import `os` is unused");
107+
assert_eq!(report, "Import `os` may be unused");
108108
}
109109

110110
#[test]
@@ -132,7 +132,7 @@ def process(items: List[str]):
132132
let (handles, state) = mk_multi_file_state(&[("main", code)], Require::Exports, true);
133133
let handle = handles.get("main").unwrap();
134134
let report = get_unused_import_diagnostics(&state, handle);
135-
assert_eq!(report, "Import `Dict` is unused");
135+
assert_eq!(report, "Import `Dict` may be unused");
136136
}
137137

138138
#[test]
@@ -384,7 +384,7 @@ import os as operating_system
384384
let (handles, state) = mk_multi_file_state(&[("main", code)], Require::Exports, true);
385385
let handle = handles.get("main").unwrap();
386386
let report = get_unused_import_diagnostics(&state, handle);
387-
assert_eq!(report, "Import `operating_system` is unused");
387+
assert_eq!(report, "Import `operating_system` may be unused");
388388
}
389389

390390
#[test]
@@ -396,5 +396,5 @@ from math import tau as my_tau
396396
let (handles, state) = mk_multi_file_state(&[("main", code)], Require::Exports, true);
397397
let handle = handles.get("main").unwrap();
398398
let report = get_unused_import_diagnostics(&state, handle);
399-
assert_eq!(report, "Import `my_tau` is unused");
399+
assert_eq!(report, "Import `my_tau` may be unused");
400400
}

pyrefly/lib/test/lsp/lsp_interaction/diagnostic.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -879,7 +879,7 @@ fn test_unused_import_diagnostic() {
879879
"items": [
880880
{
881881
"code": "unused-import",
882-
"message": "Import `os` is unused",
882+
"message": "Import `os` may be unused",
883883
"range": {
884884
"start": {"line": 6, "character": 7},
885885
"end": {"line": 6, "character": 9}
@@ -928,7 +928,7 @@ fn test_unused_from_import_diagnostic() {
928928
"items": [
929929
{
930930
"code": "unused-import",
931-
"message": "Import `Dict` is unused",
931+
"message": "Import `Dict` may be unused",
932932
"range": {
933933
"start": {"line": 6, "character": 19},
934934
"end": {"line": 6, "character": 23}
@@ -1613,7 +1613,7 @@ fn test_untyped_import_diagnostic_does_not_show_non_recommended_packages() {
16131613
"items": [
16141614
{
16151615
"code": "unused-import",
1616-
"message": "Import `boto3` is unused",
1616+
"message": "Import `boto3` may be unused",
16171617
"range": {
16181618
"start": {"line": 5, "character": 7},
16191619
"end": {"line": 5, "character": 12}
@@ -1730,7 +1730,7 @@ fn test_untyped_import_diagnostic_shows_error_for_recommended_packages() {
17301730
},
17311731
{
17321732
"code": "unused-import",
1733-
"message": "Import `django` is unused",
1733+
"message": "Import `django` may be unused",
17341734
"range": {
17351735
"start": {"line": 5, "character": 7},
17361736
"end": {"line": 5, "character": 13}

website/docs/IDE-features.mdx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,9 @@ Type errors and warnings from Pyrefly’s checker appear in the diagnostics pane
437437
preload="metadata"
438438
/>
439439

440+
Pyrefly also greys out unused imports and unused local variables directly in the editor. These are IDE-only hints and do not appear in the CLI.
441+
Because Pyrefly does not model import side effects, an import used only for its side effects, for example a plugin that registers accessors or methods on an existing class, such as `pyjanitor` extending `pandas.DataFrame`, may be greyed even though it is required at runtime. Thus the hint is "may be unused" rather than "is unused."
442+
440443
---
441444

442445
### [Semantic tokens](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_semanticTokens)

0 commit comments

Comments
 (0)