-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[ty] Fix crash when hovering an unknown string annotation #21782
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+275
−2
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -309,3 +309,39 @@ html.parser | |||
|
|
||||
| Ok(()) | ||||
| } | ||||
|
|
||||
| /// Regression test for a panic when a code-fix diagnostic points at a string annotation | ||||
| #[test] | ||||
| fn code_action_invalid_string_annotations() -> Result<()> { | ||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we make this an integration test instead of an e2e test (they're so verbose and the snapshots are somewhat hard to review). See ruff/crates/ty_ide/src/code_action.rs Line 86 in 258b1fd
|
||||
| let workspace_root = SystemPath::new("src"); | ||||
| let foo = SystemPath::new("src/foo.py"); | ||||
| let foo_content = r#" | ||||
| ab: "foobar" | ||||
| "#; | ||||
|
|
||||
| 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 for the line with the unused ignore comment. | ||||
| let code_action_id = server.send_request::<CodeActionRequest>(code_action_params); | ||||
| let code_actions = server.await_response::<CodeActionRequest>(&code_action_id); | ||||
|
|
||||
| insta::assert_json_snapshot!(code_actions); | ||||
|
|
||||
| Ok(()) | ||||
| } | ||||
52 changes: 52 additions & 0 deletions
52
...server/tests/e2e/snapshots/e2e__code_actions__code_action_invalid_string_annotations.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| --- | ||
| source: crates/ty_server/tests/e2e/code_actions.rs | ||
| expression: code_actions | ||
| --- | ||
| [ | ||
| { | ||
| "title": "Ignore 'unresolved-reference' for this line", | ||
| "kind": "quickfix", | ||
| "diagnostics": [ | ||
| { | ||
| "range": { | ||
| "start": { | ||
| "line": 1, | ||
| "character": 5 | ||
| }, | ||
| "end": { | ||
| "line": 1, | ||
| "character": 11 | ||
| } | ||
| }, | ||
| "severity": 1, | ||
| "code": "unresolved-reference", | ||
| "codeDescription": { | ||
| "href": "https://ty.dev/rules#unresolved-reference" | ||
| }, | ||
| "source": "ty", | ||
| "message": "Name `foobar` used when not defined", | ||
| "relatedInformation": [] | ||
| } | ||
| ], | ||
| "edit": { | ||
| "changes": { | ||
| "file://<temp_dir>/src/foo.py": [ | ||
| { | ||
| "range": { | ||
| "start": { | ||
| "line": 1, | ||
| "character": 12 | ||
| }, | ||
| "end": { | ||
| "line": 1, | ||
| "character": 12 | ||
| } | ||
| }, | ||
| "newText": " # ty:ignore[unresolved-reference]" | ||
| } | ||
| ] | ||
| } | ||
| }, | ||
| "isPreferred": false | ||
| } | ||
| ] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This feels rather fragile if we have to do this for many fixes, and we should explore alternative ways to handle string annotations if this becomes a frequent problem, or consider as an alternative solution for how to store types for expressions in stringified type annotations.
Something I had in mind for a long time and that can also become handy if we ever want to support checking Python embedded in other languages (e.g. Python in Markdown) is the concept of virtual files. r-a has something very similar, where code references actual code in a file OR code generated by a macro. The way they solve this is by having a
HIRFile. In our case,HIRFilewould be asalsa::Supertypeenum over:parsed_moduleandTypeInferenceBuilderwould take aHIRFileas an argument and so would other queries. Obviously, this requires more design work to flesh out the details. E.g., how does this work with looking up the scope? But it expresses the concept of nested ASTs (or even documents) in a more explicit way