Skip to content

Conversation

@MichaReiser
Copy link
Member

@MichaReiser MichaReiser commented Apr 11, 2025

Summary

Uses the relatedInformation field in diagnostics to render additional data.

For now, this only renders subdiagnostics and annotations with location information. I played with showing other annotations as well, but the rendering in VS code looked awful (I mainly played with the overload diagnostic). I think this is an improvement over the status quo and we can iterate on it as we go.

Closes astral-sh/ty#170

Test Plan

Screen.Recording.2025-05-17.at.20.34.46.mov

@MichaReiser MichaReiser added the ty Multi-file analysis & type inference label Apr 11, 2025
@github-actions
Copy link
Contributor

github-actions bot commented Apr 11, 2025

mypy_primer results

No ecosystem changes detected ✅

@MichaReiser MichaReiser force-pushed the micha/diagnostic-related-information branch 2 times, most recently from 4799b43 to 87633b3 Compare May 17, 2025 18:37
@MichaReiser MichaReiser marked this pull request as ready for review May 17, 2025 18:37
@MichaReiser MichaReiser added the server Related to the LSP server label May 17, 2025
@AlexWaygood AlexWaygood changed the title [red-knot] Show related information in diagnostic [ty] Show related information in diagnostic May 17, 2025
@MichaReiser MichaReiser force-pushed the micha/diagnostic-related-information branch from 87633b3 to 5a6035d Compare May 17, 2025 18:42
@MichaReiser MichaReiser force-pushed the micha/diagnostic-related-information branch from 5a6035d to fe74e0e Compare May 17, 2025 18:44
@carljm
Copy link
Contributor

carljm commented May 17, 2025

This looks great to me, but I'll probably leave review to @BurntSushi

@carljm carljm removed their request for review May 17, 2025 22:18
@AlexWaygood AlexWaygood requested review from BurntSushi and dhruvmanila and removed request for AlexWaygood May 17, 2025 22:18
Copy link
Member

@dhruvmanila dhruvmanila left a comment

Choose a reason for hiding this comment

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

For now, this only renders subdiagnostics and annotations with location information. I played with showing other annotations as well, but the rendering in VS code looked awful (I mainly played with the overload diagnostic).

I think specifically for overloads this seems like a reasonable amount of information to display using related information because a user can hover over the function variable to look at the possible overloads. For example, here the first group is the ty diagnostics while the last group is the hover content:

Screenshot 2025-05-18 at 10 33 49

How this plays out with other diagnostics, that's something that we can play around in follow-ups but this is a good start as you've mentioned.


I know Neovim doesn't support related information but I'm not sure about editors other than VS Code like Zed.

/// The type returned implements the `std::fmt::Display` trait. In most
/// cases, just converting it to a string (or printing it) will do what
/// you want.
pub fn concise_message(&self) -> ConciseMessage {
Copy link
Member

Choose a reason for hiding this comment

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

This looks the same as Diagnostic::concise_message. I think @BurntSushi might have better recommendation but we could use a trait to avoid the duplication.

Copy link
Member

Choose a reason for hiding this comment

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

I would actually prefer the duplication over a trait. :-)

If the duplication gets obnoxious, another path we could follow is split the common parts of Diagnostic and SubDiagnostic out into a new internal helper type, and implement what we need there. But I don't think that's quite worth doing yet.

My bias is that I tend to be pretty trait-averse.

Copy link
Member

@BurntSushi BurntSushi left a comment

Choose a reason for hiding this comment

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

This looks like a great start to me!

/// The type returned implements the `std::fmt::Display` trait. In most
/// cases, just converting it to a string (or printing it) will do what
/// you want.
pub fn concise_message(&self) -> ConciseMessage {
Copy link
Member

Choose a reason for hiding this comment

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

I would actually prefer the duplication over a trait. :-)

If the duplication gets obnoxious, another path we could follow is split the common parts of Diagnostic and SubDiagnostic out into a new internal helper type, and implement what we need there. But I don't think that's quite worth doing yet.

My bias is that I tend to be pretty trait-averse.

@MichaReiser MichaReiser merged commit 6985de4 into main May 19, 2025
35 checks passed
@MichaReiser MichaReiser deleted the micha/diagnostic-related-information branch May 19, 2025 16:52
dcreager added a commit that referenced this pull request May 19, 2025
…rals

* origin/main:
  [ty] Add hint that PEP 604 union syntax is only available in 3.10+ (#18192)
  Unify `Message` variants (#18051)
  [`airflow`] Update `AIR301` and `AIR311` with the latest Airflow implementations (#17985)
  [`airflow`] Move rules from `AIR312` to `AIR302` (#17940)
  [ty] Small LSP cleanups (#18201)
  [ty] Show related information in diagnostic (#17359)
  Default `src.root` to `['.', '<project_name>']` if the directory exists (#18141)
dcreager added a commit that referenced this pull request May 19, 2025
* main:
  [ty] Use first matching constructor overload when inferring specializations (#18204)
  [ty] Add hint that PEP 604 union syntax is only available in 3.10+ (#18192)
  Unify `Message` variants (#18051)
  [`airflow`] Update `AIR301` and `AIR311` with the latest Airflow implementations (#17985)
  [`airflow`] Move rules from `AIR312` to `AIR302` (#17940)
  [ty] Small LSP cleanups (#18201)
  [ty] Show related information in diagnostic (#17359)
  Default `src.root` to `['.', '<project_name>']` if the directory exists (#18141)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

server Related to the LSP server ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Multi-span diagnostics in the LSP

5 participants