From b813d915b5d52272c9ac0649d146ae4f1c6abbf2 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Fri, 30 Aug 2024 19:18:59 -0400 Subject: [PATCH] Mark sys.version_info[0] < 3 as outdated --- .../test/fixtures/pyupgrade/UP036_0.py | 30 +++ .../pyupgrade/rules/outdated_version_block.rs | 88 ++++---- ...__rules__pyupgrade__tests__UP036_0.py.snap | 204 ++++++++++++++++++ 3 files changed, 285 insertions(+), 37 deletions(-) diff --git a/crates/ruff_linter/resources/test/fixtures/pyupgrade/UP036_0.py b/crates/ruff_linter/resources/test/fixtures/pyupgrade/UP036_0.py index 50d499b872bb4..921c6b7c2cf45 100644 --- a/crates/ruff_linter/resources/test/fixtures/pyupgrade/UP036_0.py +++ b/crates/ruff_linter/resources/test/fixtures/pyupgrade/UP036_0.py @@ -225,3 +225,33 @@ def g(): "this is\ allowed too" + +if sys.version_info[0] == 3: + print("py3") + +if sys.version_info[0] <= 3: + print("py3") + +if sys.version_info[0] < 3: + print("py3") + +if sys.version_info[0] >= 3: + print("py3") + +if sys.version_info[0] > 3: + print("py3") + +if sys.version_info[0] == 2: + print("py3") + +if sys.version_info[0] <= 2: + print("py3") + +if sys.version_info[0] < 2: + print("py3") + +if sys.version_info[0] >= 2: + print("py3") + +if sys.version_info[0] > 2: + print("py3") diff --git a/crates/ruff_linter/src/rules/pyupgrade/rules/outdated_version_block.rs b/crates/ruff_linter/src/rules/pyupgrade/rules/outdated_version_block.rs index 04391303d2caa..ca7faba02cdd5 100644 --- a/crates/ruff_linter/src/rules/pyupgrade/rules/outdated_version_block.rs +++ b/crates/ruff_linter/src/rules/pyupgrade/rules/outdated_version_block.rs @@ -57,7 +57,9 @@ impl Violation for OutdatedVersionBlock { fn message(&self) -> String { let OutdatedVersionBlock { reason } = self; match reason { - Reason::Outdated => format!("Version block is outdated for minimum Python version"), + Reason::AlwaysFalse | Reason::AlwaysTrue => { + format!("Version block is outdated for minimum Python version") + } Reason::Invalid => format!("Version specifier is invalid"), } } @@ -65,7 +67,9 @@ impl Violation for OutdatedVersionBlock { fn fix_title(&self) -> Option { let OutdatedVersionBlock { reason } = self; match reason { - Reason::Outdated => Some("Remove outdated version block".to_string()), + Reason::AlwaysFalse | Reason::AlwaysTrue => { + Some("Remove outdated version block".to_string()) + } Reason::Invalid => None, } } @@ -73,7 +77,8 @@ impl Violation for OutdatedVersionBlock { #[derive(Debug, PartialEq, Eq)] enum Reason { - Outdated, + AlwaysTrue, + AlwaysFalse, Invalid, } @@ -123,7 +128,11 @@ pub(crate) fn outdated_version_block(checker: &mut Checker, stmt_if: &StmtIf) { Ok(true) => { let mut diagnostic = Diagnostic::new( OutdatedVersionBlock { - reason: Reason::Outdated, + reason: if op.is_lt() || op.is_lt_e() { + Reason::AlwaysFalse + } else { + Reason::AlwaysTrue + }, }, branch.test.range(), ); @@ -152,41 +161,46 @@ pub(crate) fn outdated_version_block(checker: &mut Checker, stmt_if: &StmtIf) { value: ast::Number::Int(int), .. }) => { - if op == &CmpOp::Eq { - match int.as_u8() { - Some(2) => { - let mut diagnostic = Diagnostic::new( - OutdatedVersionBlock { - reason: Reason::Outdated, - }, - branch.test.range(), - ); - if let Some(fix) = fix_always_false_branch(checker, stmt_if, &branch) { - diagnostic.set_fix(fix); - } - checker.diagnostics.push(diagnostic); + let reason = match (int.as_u8(), op) { + (Some(2), CmpOp::Eq) => Reason::AlwaysFalse, + (Some(3), CmpOp::Eq) => Reason::AlwaysTrue, + (Some(2), CmpOp::NotEq) => Reason::AlwaysTrue, + (Some(3), CmpOp::NotEq) => Reason::AlwaysFalse, + (Some(2), CmpOp::Lt) => Reason::AlwaysFalse, + (Some(3), CmpOp::Lt) => Reason::AlwaysFalse, + (Some(2), CmpOp::LtE) => Reason::AlwaysFalse, + (Some(3), CmpOp::LtE) => Reason::AlwaysTrue, + (Some(2), CmpOp::Gt) => Reason::AlwaysTrue, + (Some(3), CmpOp::Gt) => Reason::AlwaysFalse, + (Some(2), CmpOp::GtE) => Reason::AlwaysTrue, + (Some(3), CmpOp::GtE) => Reason::AlwaysTrue, + (None, _) => Reason::Invalid, + _ => return, + }; + match reason { + Reason::AlwaysTrue => { + let mut diagnostic = + Diagnostic::new(OutdatedVersionBlock { reason }, branch.test.range()); + if let Some(fix) = fix_always_true_branch(checker, stmt_if, &branch) { + diagnostic.set_fix(fix); } - Some(3) => { - let mut diagnostic = Diagnostic::new( - OutdatedVersionBlock { - reason: Reason::Outdated, - }, - branch.test.range(), - ); - if let Some(fix) = fix_always_true_branch(checker, stmt_if, &branch) { - diagnostic.set_fix(fix); - } - checker.diagnostics.push(diagnostic); - } - None => { - checker.diagnostics.push(Diagnostic::new( - OutdatedVersionBlock { - reason: Reason::Invalid, - }, - comparison.range(), - )); + checker.diagnostics.push(diagnostic); + } + Reason::AlwaysFalse => { + let mut diagnostic = + Diagnostic::new(OutdatedVersionBlock { reason }, branch.test.range()); + if let Some(fix) = fix_always_false_branch(checker, stmt_if, &branch) { + diagnostic.set_fix(fix); } - _ => {} + checker.diagnostics.push(diagnostic); + } + Reason::Invalid => { + checker.diagnostics.push(Diagnostic::new( + OutdatedVersionBlock { + reason: Reason::Invalid, + }, + comparison.range(), + )); } } } diff --git a/crates/ruff_linter/src/rules/pyupgrade/snapshots/ruff_linter__rules__pyupgrade__tests__UP036_0.py.snap b/crates/ruff_linter/src/rules/pyupgrade/snapshots/ruff_linter__rules__pyupgrade__tests__UP036_0.py.snap index 329642fd56850..fbfadc12583c0 100644 --- a/crates/ruff_linter/src/rules/pyupgrade/snapshots/ruff_linter__rules__pyupgrade__tests__UP036_0.py.snap +++ b/crates/ruff_linter/src/rules/pyupgrade/snapshots/ruff_linter__rules__pyupgrade__tests__UP036_0.py.snap @@ -801,3 +801,207 @@ UP036_0.py:219:4: UP036 [*] Version block is outdated for minimum Python version 226 |- "this is\ 225 |+"this is\ 227 226 | allowed too" +228 227 | +229 228 | if sys.version_info[0] == 3: + +UP036_0.py:229:4: UP036 [*] Version block is outdated for minimum Python version + | +227 | allowed too" +228 | +229 | if sys.version_info[0] == 3: + | ^^^^^^^^^^^^^^^^^^^^^^^^ UP036 +230 | print("py3") + | + = help: Remove outdated version block + +ℹ Unsafe fix +226 226 | "this is\ +227 227 | allowed too" +228 228 | +229 |-if sys.version_info[0] == 3: +230 |- print("py3") + 229 |+print("py3") +231 230 | +232 231 | if sys.version_info[0] <= 3: +233 232 | print("py3") + +UP036_0.py:232:4: UP036 [*] Version block is outdated for minimum Python version + | +230 | print("py3") +231 | +232 | if sys.version_info[0] <= 3: + | ^^^^^^^^^^^^^^^^^^^^^^^^ UP036 +233 | print("py3") + | + = help: Remove outdated version block + +ℹ Unsafe fix +229 229 | if sys.version_info[0] == 3: +230 230 | print("py3") +231 231 | +232 |-if sys.version_info[0] <= 3: +233 |- print("py3") + 232 |+print("py3") +234 233 | +235 234 | if sys.version_info[0] < 3: +236 235 | print("py3") + +UP036_0.py:235:4: UP036 [*] Version block is outdated for minimum Python version + | +233 | print("py3") +234 | +235 | if sys.version_info[0] < 3: + | ^^^^^^^^^^^^^^^^^^^^^^^ UP036 +236 | print("py3") + | + = help: Remove outdated version block + +ℹ Unsafe fix +232 232 | if sys.version_info[0] <= 3: +233 233 | print("py3") +234 234 | +235 |-if sys.version_info[0] < 3: +236 |- print("py3") +237 235 | +238 236 | if sys.version_info[0] >= 3: +239 237 | print("py3") + +UP036_0.py:238:4: UP036 [*] Version block is outdated for minimum Python version + | +236 | print("py3") +237 | +238 | if sys.version_info[0] >= 3: + | ^^^^^^^^^^^^^^^^^^^^^^^^ UP036 +239 | print("py3") + | + = help: Remove outdated version block + +ℹ Unsafe fix +235 235 | if sys.version_info[0] < 3: +236 236 | print("py3") +237 237 | +238 |-if sys.version_info[0] >= 3: +239 |- print("py3") + 238 |+print("py3") +240 239 | +241 240 | if sys.version_info[0] > 3: +242 241 | print("py3") + +UP036_0.py:241:4: UP036 [*] Version block is outdated for minimum Python version + | +239 | print("py3") +240 | +241 | if sys.version_info[0] > 3: + | ^^^^^^^^^^^^^^^^^^^^^^^ UP036 +242 | print("py3") + | + = help: Remove outdated version block + +ℹ Unsafe fix +238 238 | if sys.version_info[0] >= 3: +239 239 | print("py3") +240 240 | +241 |-if sys.version_info[0] > 3: +242 |- print("py3") +243 241 | +244 242 | if sys.version_info[0] == 2: +245 243 | print("py3") + +UP036_0.py:244:4: UP036 [*] Version block is outdated for minimum Python version + | +242 | print("py3") +243 | +244 | if sys.version_info[0] == 2: + | ^^^^^^^^^^^^^^^^^^^^^^^^ UP036 +245 | print("py3") + | + = help: Remove outdated version block + +ℹ Unsafe fix +241 241 | if sys.version_info[0] > 3: +242 242 | print("py3") +243 243 | +244 |-if sys.version_info[0] == 2: +245 |- print("py3") +246 244 | +247 245 | if sys.version_info[0] <= 2: +248 246 | print("py3") + +UP036_0.py:247:4: UP036 [*] Version block is outdated for minimum Python version + | +245 | print("py3") +246 | +247 | if sys.version_info[0] <= 2: + | ^^^^^^^^^^^^^^^^^^^^^^^^ UP036 +248 | print("py3") + | + = help: Remove outdated version block + +ℹ Unsafe fix +244 244 | if sys.version_info[0] == 2: +245 245 | print("py3") +246 246 | +247 |-if sys.version_info[0] <= 2: +248 |- print("py3") +249 247 | +250 248 | if sys.version_info[0] < 2: +251 249 | print("py3") + +UP036_0.py:250:4: UP036 [*] Version block is outdated for minimum Python version + | +248 | print("py3") +249 | +250 | if sys.version_info[0] < 2: + | ^^^^^^^^^^^^^^^^^^^^^^^ UP036 +251 | print("py3") + | + = help: Remove outdated version block + +ℹ Unsafe fix +247 247 | if sys.version_info[0] <= 2: +248 248 | print("py3") +249 249 | +250 |-if sys.version_info[0] < 2: +251 |- print("py3") +252 250 | +253 251 | if sys.version_info[0] >= 2: +254 252 | print("py3") + +UP036_0.py:253:4: UP036 [*] Version block is outdated for minimum Python version + | +251 | print("py3") +252 | +253 | if sys.version_info[0] >= 2: + | ^^^^^^^^^^^^^^^^^^^^^^^^ UP036 +254 | print("py3") + | + = help: Remove outdated version block + +ℹ Unsafe fix +250 250 | if sys.version_info[0] < 2: +251 251 | print("py3") +252 252 | +253 |-if sys.version_info[0] >= 2: +254 |- print("py3") + 253 |+print("py3") +255 254 | +256 255 | if sys.version_info[0] > 2: +257 256 | print("py3") + +UP036_0.py:256:4: UP036 [*] Version block is outdated for minimum Python version + | +254 | print("py3") +255 | +256 | if sys.version_info[0] > 2: + | ^^^^^^^^^^^^^^^^^^^^^^^ UP036 +257 | print("py3") + | + = help: Remove outdated version block + +ℹ Unsafe fix +253 253 | if sys.version_info[0] >= 2: +254 254 | print("py3") +255 255 | +256 |-if sys.version_info[0] > 2: +257 |- print("py3") + 256 |+print("py3")