From a0b18bc153d511caf9d7a47d861ce741fa592324 Mon Sep 17 00:00:00 2001 From: Micha Reiser Date: Tue, 9 Dec 2025 14:08:22 +0100 Subject: [PATCH] [ty] Fix reveal-type E2E test (#21865) --- crates/ty_ide/src/code_action.rs | 39 +++++++- crates/ty_server/tests/e2e/code_actions.rs | 36 ------- ...ns__code_action_undefined_reveal_type.snap | 98 ------------------- 3 files changed, 38 insertions(+), 135 deletions(-) delete mode 100644 crates/ty_server/tests/e2e/snapshots/e2e__code_actions__code_action_undefined_reveal_type.snap diff --git a/crates/ty_ide/src/code_action.rs b/crates/ty_ide/src/code_action.rs index 8826dfce06..2b6703afac 100644 --- a/crates/ty_ide/src/code_action.rs +++ b/crates/ty_ide/src/code_action.rs @@ -86,7 +86,10 @@ mod tests { use ruff_diagnostics::Fix; use ruff_text_size::{TextRange, TextSize}; use ty_project::ProjectMetadata; - use ty_python_semantic::{lint::LintMetadata, types::UNRESOLVED_REFERENCE}; + use ty_python_semantic::{ + lint::LintMetadata, + types::{UNDEFINED_REVEAL, UNRESOLVED_REFERENCE}, + }; #[test] fn add_ignore() { @@ -441,6 +444,40 @@ mod tests { "#); } + #[test] + fn undefined_reveal_type() { + let test = CodeActionTest::with_source( + r#" + reveal_type(1) + "#, + ); + + assert_snapshot!(test.code_actions(&UNDEFINED_REVEAL), @r" + info[code-action]: import typing.reveal_type + --> main.py:2:13 + | + 2 | reveal_type(1) + | ^^^^^^^^^^^ + | + help: This is a preferred code action + 1 + from typing import reveal_type + 2 | + 3 | reveal_type(1) + 4 | + + info[code-action]: Ignore 'undefined-reveal' for this line + --> main.py:2:13 + | + 2 | reveal_type(1) + | ^^^^^^^^^^^ + | + 1 | + - reveal_type(1) + 2 + reveal_type(1) # ty:ignore[undefined-reveal] + 3 | + "); + } + pub(super) struct CodeActionTest { pub(super) db: ty_project::TestDb, pub(super) file: File, diff --git a/crates/ty_server/tests/e2e/code_actions.rs b/crates/ty_server/tests/e2e/code_actions.rs index d3d50c5fb9..8d8c1daebe 100644 --- a/crates/ty_server/tests/e2e/code_actions.rs +++ b/crates/ty_server/tests/e2e/code_actions.rs @@ -158,42 +158,6 @@ x: Literal[1] = 1 Ok(()) } -// `Literal` is available from two places so we should suggest two possible imports -#[test] -fn code_action_undefined_reveal_type() -> Result<()> { - let workspace_root = SystemPath::new("src"); - let foo = SystemPath::new("src/foo.py"); - let foo_content = "\ -reveal_type(1) -"; - - let ty_toml = SystemPath::new("ty.toml"); - let ty_toml_content = ""; - - let mut server = TestServerBuilder::new()? - .with_workspace(workspace_root, None)? - .with_file(ty_toml, ty_toml_content)? - .with_file(foo, foo_content)? - .enable_pull_diagnostics(true) - .build() - .wait_until_workspaces_are_initialized(); - - server.open_text_document(foo, foo_content, 1); - - // Wait for diagnostics to be computed. - let diagnostics = server.document_diagnostic_request(foo, None); - let range = full_range(foo_content); - let code_action_params = code_actions_at(&server, diagnostics, foo, range); - - // Get code actions - let code_action_id = server.send_request::(code_action_params); - let code_actions = server.await_response::(&code_action_id); - - insta::assert_json_snapshot!(code_actions); - - Ok(()) -} - // Using an unimported decorator `@deprecated` #[test] fn code_action_undefined_decorator() -> Result<()> { diff --git a/crates/ty_server/tests/e2e/snapshots/e2e__code_actions__code_action_undefined_reveal_type.snap b/crates/ty_server/tests/e2e/snapshots/e2e__code_actions__code_action_undefined_reveal_type.snap deleted file mode 100644 index aace2bc042..0000000000 --- a/crates/ty_server/tests/e2e/snapshots/e2e__code_actions__code_action_undefined_reveal_type.snap +++ /dev/null @@ -1,98 +0,0 @@ ---- -source: crates/ty_server/tests/e2e/code_actions.rs -expression: code_actions ---- -[ - { - "title": "import typing.reveal_type", - "kind": "quickfix", - "diagnostics": [ - { - "range": { - "start": { - "line": 0, - "character": 0 - }, - "end": { - "line": 0, - "character": 11 - } - }, - "severity": 2, - "code": "undefined-reveal", - "codeDescription": { - "href": "https://ty.dev/rules#undefined-reveal" - }, - "source": "ty", - "message": "`reveal_type` used without importing it", - "relatedInformation": [] - } - ], - "edit": { - "changes": { - "file:///src/foo.py": [ - { - "range": { - "start": { - "line": 0, - "character": 0 - }, - "end": { - "line": 0, - "character": 0 - } - }, - "newText": "from typing import reveal_type\n" - } - ] - } - }, - "isPreferred": true - }, - { - "title": "Ignore 'undefined-reveal' for this line", - "kind": "quickfix", - "diagnostics": [ - { - "range": { - "start": { - "line": 0, - "character": 0 - }, - "end": { - "line": 0, - "character": 11 - } - }, - "severity": 2, - "code": "undefined-reveal", - "codeDescription": { - "href": "https://ty.dev/rules#undefined-reveal" - }, - "source": "ty", - "message": "`reveal_type` used without importing it", - "relatedInformation": [] - } - ], - "edit": { - "changes": { - "file:///src/foo.py": [ - { - "range": { - "start": { - "line": 0, - "character": 14 - }, - "end": { - "line": 0, - "character": 14 - } - }, - "newText": " # ty:ignore[undefined-reveal]" - } - ] - } - }, - "isPreferred": false - } -]