Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[pyupgrade] Improve diagnostic range for tuples (UP024)#23013

Merged
ntBre merged 1 commit intoastral-sh:mainfrom
Jkhall81:fix/up024-highlighting-19696
Feb 5, 2026
Merged

[pyupgrade] Improve diagnostic range for tuples (UP024)#23013
ntBre merged 1 commit intoastral-sh:mainfrom
Jkhall81:fix/up024-highlighting-19696

Conversation

@Jkhall81
Copy link
Contributor

@Jkhall81 Jkhall81 commented Feb 2, 2026

Summary

This PR refines the diagnostic highlighting for UP024. Previously, when multiple exceptions were in a tuple, the linter would underline the entire tuple. Now specific aliases within a tuple with be highlighted.

Fixes #19696

@astral-sh-bot
Copy link

astral-sh-bot bot commented Feb 2, 2026

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Copy link
Contributor

@ntBre ntBre left a comment

Choose a reason for hiding this comment

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

Thanks for working on this! I think we need to be more careful with the diagnostic range because we will currently limit it to the first alias, even if there are multiple aliases in the same tuple. I think we should try to use a secondary_annotation to underline each of the other aliases too.

I also think this is likely to be a breaking change. The start of the diagnostic range is used for noqa suppressions, so a comment like this currently suppresses the rule:

try: ...
except ( # noqa: UP024
    IOError, ValueError, IOError): ...

I think this will not be the case after this PR, so we should make this a preview change, especially if you can confirm that the noqa range changes.

@ntBre ntBre added the diagnostics Related to reporting of diagnostics. label Feb 2, 2026
@Jkhall81 Jkhall81 force-pushed the fix/up024-highlighting-19696 branch from 71881f0 to 80844d8 Compare February 3, 2026 02:01
let Some(first_alias) = aliases.first() else {
return;
};
let diagnostic_range = if checker.settings().preview.is_enabled() {
Copy link
Member

Choose a reason for hiding this comment

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

We prefer to add a new is_<feature_name>_enabled() helper to ruff_linter/src/preview.rs rather than simply checking if preview is enabled.

https://github.com/astral-sh/ruff/blob/main/crates/ruff_linter/src/preview.rs

tuple.range()
};
let mut diagnostic = checker.report_diagnostic(OSErrorAlias { name: None }, diagnostic_range);
if checker.settings().preview.is_enabled() {
Copy link
Member

Choose a reason for hiding this comment

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

Same here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My bad. Thank you for pointing this out. Added a preview wrapper function to preview.rs and then updated these two spots.

@Jkhall81 Jkhall81 force-pushed the fix/up024-highlighting-19696 branch from 80844d8 to f213d1a Compare February 4, 2026 01:49
@Jkhall81 Jkhall81 requested a review from amyreese February 4, 2026 03:22
Copy link
Contributor

@ntBre ntBre left a comment

Choose a reason for hiding this comment

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

Thank you! I had a couple more small suggestions, but this looks good to me.

@ntBre ntBre added the preview Related to preview mode features label Feb 4, 2026
@Jkhall81 Jkhall81 force-pushed the fix/up024-highlighting-19696 branch from f213d1a to 3eeba8f Compare February 5, 2026 12:16
@Jkhall81 Jkhall81 force-pushed the fix/up024-highlighting-19696 branch from 3eeba8f to 74009d3 Compare February 5, 2026 12:23
@Jkhall81 Jkhall81 requested a review from ntBre February 5, 2026 12:39
Copy link
Contributor

@ntBre ntBre left a comment

Choose a reason for hiding this comment

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

Thank you!

@ntBre ntBre dismissed amyreese’s stale review February 5, 2026 14:41

Requested changes have been made

@ntBre ntBre changed the title pyupgrade: tighten diagnostic range for UP024 tuples (#19696 [pyupgrade] Improve diagnostic range for tuples (UP024) Feb 5, 2026
@ntBre ntBre merged commit 6a5ff8e into astral-sh:main Feb 5, 2026
41 checks passed
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. preview Related to preview mode features

Projects

None yet

Development

Successfully merging this pull request may close these issues.

UP024 error message is confusing when except clause has multiple exceptions

3 participants