-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[pyupgrade] Add sub-diagnostics showing type variable definitions (UP046)
#19886
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
Conversation
…`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:
|
I think what we do in ty is that help texts and notes are always the last sub-diagnostics. |
|
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. |
…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


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::infomethod 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:
ruff/crates/ruff_linter/src/test.rs
Lines 280 to 288 in 79c949f
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:
Alternative Idea
I don't really love the
help:andinfo:lines stacked so closely together, so I also tried this as a secondary annotation: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).