Skip to content

Conversation

@ntBre
Copy link
Contributor

@ntBre ntBre commented Aug 12, 2025

Summary

Now that we've handled caching for the new diagnostics, I went looking for diagnostics that could use additional annotations or sub-diagnostics that weren't already tracked in #17203. UP046 (and UP040 and UP047 if we like this one) seemed like a reasonable candidate because we check for separate type variable definitions in the same file before emitting a diagnostic. In this case, I added an info-level sub-diagnostic pointing to this type variable definition. I'm not actually sure if this is super helpful information, so I wouldn't mind closing this if others don't think it's useful.

I also added a helper DiagnosticGuard::info method that I think will be useful for adding this kind of sub-diagnostic in general.

I spent a while debugging why the snapshots were showing a longer path in the sub-diagnostics before remembering that we overwrite the source file for the main diagnostic here:

// Not strictly necessary but adds some coverage for this code path by overriding the
// noqa offset and the source file
let range = diagnostic.expect_range();
diagnostic.set_noqa_offset(directives.noqa_line_for.resolve(range.start()));
if let Some(annotation) = diagnostic.primary_annotation_mut() {
annotation.set_span(
ruff_db::diagnostic::Span::from(source_code.clone()).with_range(range),
);
}

As shown below, the sub-diagnostics look as expected in the CLI. I think we could delete this part of the test code, but it results in using the longer test file names (like our new sub-diagnostics) in every snapshot. So alternatively, we may want to apply the same transformation to the sub-diagnostics (and secondary annotations) too.

Test Plan

Existing UP046 tests with newly expanded snapshots, as well as some manual testing in the CLI:

image

Alternative Idea

I don't really love the help: and info: lines stacked so closely together, so I also tried this as a secondary annotation:

image

That's not implemented in the PR but could be a different route with a small change to DiagnosticGuard::info (and a rename of the method).

…`UP046`)

Summary
--

Now that we've handled caching for the new diagnostics, I went looking for
diagnostics that could use additional annotations or sub-diagnostics that
weren't already tracked in #17203. UP046 (and UP040 and UP047 if we like this
one) seemed like a reasonable candidate because we check for separate type
variable definitions in the same file before emitting a diagnostic. In this
case, I added an info-level sub-diagnostic pointing to this type variable
definition. I'm not actually sure if this is super helpful information, so I
wouldn't mind closing this if others don't think it's useful.

I also added a helper `DiagnosticGuard::info` method that I think will be useful
for adding this kind of sub-diagnostic in general.

I spent a while debugging why the snapshots were showing a longer path in the
sub-diagnostics before remembering that we overwrite the source file for the
main diagnostic here:

https://github.com/astral-sh/ruff/blob/79c949f0f75f6582782f3d5fddc65089789801d5/crates/ruff_linter/src/test.rs#L280-L288

As shown below, the sub-diagnostics look as expected in the CLI. I think we
_could_ delete this part of the test code, but it results in using the longer
test file names (like our new sub-diagnostics) in every snapshot. So
alternatively, we may want to apply the same transformation to the
sub-diagnostics (and secondary annotations) too.

Test Plan
--

Existing UP046 tests with newly expanded snapshots, as well as some manual
testing in the CLI:
@github-actions
Copy link
Contributor

github-actions bot commented Aug 12, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@ntBre
Copy link
Contributor Author

ntBre commented Aug 12, 2025

Ah, I guess we need to replace all the paths to make the tests work on Windows anyway.

Also, I'm starting to think the secondary annotation might be a lot better, especially for multiple type variables:

image

@MichaReiser
Copy link
Member

MichaReiser commented Aug 13, 2025

I don't really love the help: and info: lines stacked so closely together, so I also tried this as a secondary annotation:

I think what we do in ty is that help texts and notes are always the last sub-diagnostics.

@AlexWaygood AlexWaygood added the diagnostics Related to reporting of diagnostics. label Aug 13, 2025
@ntBre
Copy link
Contributor Author

ntBre commented Aug 13, 2025

Ah, that does look better already. Thanks!

image

I just did this by sorting the sub-diagnostics in our DiagnosticGuard Drop impl. That seemed like the easiest fix since we push the help diagnostic right when creating the Diagnostic from a Violation, but we might want a better solution longer-term.

@ntBre
Copy link
Contributor Author

ntBre commented Aug 13, 2025

I talked with Alex on Discord, and he echoed my concern that this wasn't a very useful piece of information to show to users. I'll go ahead and close this for now and hope to use some of the more general helper code for a different diagnostic.

@ntBre ntBre closed this Aug 13, 2025
ntBre added a commit that referenced this pull request Aug 14, 2025
…811`) (#19900)

## Summary

This is a second attempt at a first use of a new diagnostic feature
after #19886. I'll blame rustc for this one because it also has a
similar diagnostic:

<img width="735" height="335" alt="image"
src="https://github.com/user-attachments/assets/572fe1c3-1742-4ce4-b575-1d9196ff0932"
/>

We end up with a very similar diagnostic:

<img width="764" height="401" alt="image"
src="https://github.com/user-attachments/assets/01eaf0c7-2567-467b-a5d8-a27206b2c74c"
/>

## Test Plan

New snapshots and manual tests above
@ntBre ntBre deleted the brent/up046-subdiagnostics branch August 25, 2025 17:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

diagnostics Related to reporting of diagnostics.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants