From 4be2b38ab7885076aad7687145478bcbac688d70 Mon Sep 17 00:00:00 2001 From: Aria Desires Date: Sun, 14 Dec 2025 18:19:18 -0500 Subject: [PATCH] add tests --- crates/ty_ide/src/code_action.rs | 130 +++++++++++++++++++++++++++++++ 1 file changed, 130 insertions(+) diff --git a/crates/ty_ide/src/code_action.rs b/crates/ty_ide/src/code_action.rs index 3fcb33703e..4688e2c225 100644 --- a/crates/ty_ide/src/code_action.rs +++ b/crates/ty_ide/src/code_action.rs @@ -579,6 +579,136 @@ mod tests { "#); } + // using `importlib.abc.ExecutionLoader` when no imports are in scope + #[test] + fn unresolved_loader() { + let test = CodeActionTest::with_source( + r#" + ExecutionLoader + "#, + ); + + assert_snapshot!(test.code_actions(&UNRESOLVED_REFERENCE), @r" + info[code-action]: import importlib.abc.ExecutionLoader + --> main.py:2:13 + | + 2 | ExecutionLoader + | ^^^^^^^^^^^^^^^ + | + help: This is a preferred code action + 1 + from importlib.abc import ExecutionLoader + 2 | + 3 | ExecutionLoader + 4 | + + info[code-action]: Ignore 'unresolved-reference' for this line + --> main.py:2:13 + | + 2 | ExecutionLoader + | ^^^^^^^^^^^^^^^ + | + 1 | + - ExecutionLoader + 2 + ExecutionLoader # ty:ignore[unresolved-reference] + 3 | + "); + } + + // using `importlib.abc.ExecutionLoader` when `import importlib` is in scope + // + // TODO: `importlib.abc` is available whenever `importlib` is, so qualifying + // `importlib.abc.ExecutionLoader` without adding imports is actually legal here! + #[test] + fn unresolved_loader_importlib_imported() { + let test = CodeActionTest::with_source( + r#" + import importlib + ExecutionLoader + "#, + ); + + assert_snapshot!(test.code_actions(&UNRESOLVED_REFERENCE), @r" + info[code-action]: import importlib.abc.ExecutionLoader + --> main.py:3:13 + | + 2 | import importlib + 3 | ExecutionLoader + | ^^^^^^^^^^^^^^^ + | + help: This is a preferred code action + 1 + from importlib.abc import ExecutionLoader + 2 | + 3 | import importlib + 4 | ExecutionLoader + + info[code-action]: Ignore 'unresolved-reference' for this line + --> main.py:3:13 + | + 2 | import importlib + 3 | ExecutionLoader + | ^^^^^^^^^^^^^^^ + | + 1 | + 2 | import importlib + - ExecutionLoader + 3 + ExecutionLoader # ty:ignore[unresolved-reference] + 4 | + "); + } + + // Using `importlib.abc.ExecutionLoader` when `import importlib.abc` is in scope + #[test] + fn unresolved_loader_abc_imported() { + let test = CodeActionTest::with_source( + r#" + import importlib.abc + ExecutionLoader + "#, + ); + + assert_snapshot!(test.code_actions(&UNRESOLVED_REFERENCE), @r" + info[code-action]: import importlib.abc.ExecutionLoader + --> main.py:3:13 + | + 2 | import importlib.abc + 3 | ExecutionLoader + | ^^^^^^^^^^^^^^^ + | + help: This is a preferred code action + 1 + from importlib.abc import ExecutionLoader + 2 | + 3 | import importlib.abc + 4 | ExecutionLoader + + info[code-action]: qualify importlib.abc.ExecutionLoader + --> main.py:3:13 + | + 2 | import importlib.abc + 3 | ExecutionLoader + | ^^^^^^^^^^^^^^^ + | + help: This is a preferred code action + 1 | + 2 | import importlib.abc + - ExecutionLoader + 3 + importlib.abc.ExecutionLoader + 4 | + + info[code-action]: Ignore 'unresolved-reference' for this line + --> main.py:3:13 + | + 2 | import importlib.abc + 3 | ExecutionLoader + | ^^^^^^^^^^^^^^^ + | + 1 | + 2 | import importlib.abc + - ExecutionLoader + 3 + ExecutionLoader # ty:ignore[unresolved-reference] + 4 | + "); + } + pub(super) struct CodeActionTest { pub(super) db: ty_project::TestDb, pub(super) file: File,