[pyupgrade] Improve diagnostic range for tuples (UP024)#23013
[pyupgrade] Improve diagnostic range for tuples (UP024)#23013ntBre merged 1 commit intoastral-sh:mainfrom
pyupgrade] Improve diagnostic range for tuples (UP024)#23013Conversation
|
ntBre
left a comment
There was a problem hiding this comment.
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.
71881f0 to
80844d8
Compare
| let Some(first_alias) = aliases.first() else { | ||
| return; | ||
| }; | ||
| let diagnostic_range = if checker.settings().preview.is_enabled() { |
There was a problem hiding this comment.
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() { |
There was a problem hiding this comment.
My bad. Thank you for pointing this out. Added a preview wrapper function to preview.rs and then updated these two spots.
80844d8 to
f213d1a
Compare
ntBre
left a comment
There was a problem hiding this comment.
Thank you! I had a couple more small suggestions, but this looks good to me.
f213d1a to
3eeba8f
Compare
3eeba8f to
74009d3
Compare
pyupgrade] Improve diagnostic range for tuples (UP024)
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