Skip to content

Commit fc54409

Browse files
committed
[pyupgrade]: Improve diagnostic range for redundant-open-mode (UP015) (#16672)
## Summary This PR stabilizes the behavior change introduced in #15872 The diagnostic range is now the range of the redundant `mode` argument where it previously was the range of the entire `open` call: Before: ``` UP015.py:2:1: UP015 [*] Unnecessary mode argument | 1 | open("foo", "U") 2 | open("foo", "Ur") | ^^^^^^^^^^^^^^^^^ UP015 3 | open("foo", "Ub") 4 | open("foo", "rUb") | = help: Remove mode argument ``` Now: ``` UP015.py:2:13: UP015 [*] Unnecessary mode argument | 1 | open("foo", "U") 2 | open("foo", "Ur") | ^^^^ UP015 3 | open("foo", "Ub") 4 | open("foo", "rUb") | = help: Remove mode argument ``` This is a breaking change because it may require moving a `noqa` comment onto a different line, e.g if you have ```py open( "foo", "Ur", ) # noqa: UP015 ``` Needs to be rewritten to ```py open( "foo", "Ur", # noqa: UP015 ) ``` There have been now new issues or PRs since the new preview behavior was implemented. It first was released as part of Ruff 0.9.5 on the 5th of Feb (a little more than a month ago) ## Test Plan I reviewed the snapshot tests
1 parent 629f42d commit fc54409

6 files changed

+99
-1124
lines changed

crates/ruff_linter/src/rules/pyupgrade/mod.rs

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -119,25 +119,6 @@ mod tests {
119119
Ok(())
120120
}
121121

122-
#[test_case(Rule::RedundantOpenModes, Path::new("UP015.py"))]
123-
#[test_case(Rule::RedundantOpenModes, Path::new("UP015_1.py"))]
124-
fn preview_rules(rule_code: Rule, path: &Path) -> Result<()> {
125-
let snapshot = format!(
126-
"preview__{}_{}",
127-
rule_code.noqa_code(),
128-
path.to_string_lossy()
129-
);
130-
let diagnostics = test_path(
131-
Path::new("pyupgrade").join(path).as_path(),
132-
&settings::LinterSettings {
133-
preview: PreviewMode::Enabled,
134-
..settings::LinterSettings::for_rule(rule_code)
135-
},
136-
)?;
137-
assert_messages!(snapshot, diagnostics);
138-
Ok(())
139-
}
140-
141122
#[test]
142123
fn up007_preview() -> Result<()> {
143124
let diagnostics = test_path(

crates/ruff_linter/src/rules/pyupgrade/rules/redundant_open_modes.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,17 +91,11 @@ fn create_diagnostic(
9191
mode: OpenMode,
9292
checker: &Checker,
9393
) -> Diagnostic {
94-
let range = if checker.settings.preview.is_enabled() {
95-
mode_arg.range()
96-
} else {
97-
call.range
98-
};
99-
10094
let mut diagnostic = Diagnostic::new(
10195
RedundantOpenModes {
10296
replacement: mode.to_string(),
10397
},
104-
range,
98+
mode_arg.range(),
10599
);
106100

107101
if mode.is_empty() {

0 commit comments

Comments
 (0)