Skip to content
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

Mark sys.version_info[0] < 3 and similar comparisons as outdated #13175

Merged
merged 1 commit into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/pyupgrade/UP036_0.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,28 @@ 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"),
}
}

fn fix_title(&self) -> Option<String> {
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,
}
}
}

#[derive(Debug, PartialEq, Eq)]
enum Reason {
Outdated,
AlwaysTrue,
AlwaysFalse,
Invalid,
}

Expand Down Expand Up @@ -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(),
);
Expand Down Expand Up @@ -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,
Copy link
Member Author

Choose a reason for hiding this comment

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

== 3 is maybe a little dubious, but we were already doing it. (== is the only case we handled before.)

(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(),
));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Loading