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

Detect sys.version_info slices in outdated-version-block #8112

Merged
merged 1 commit into from
Oct 21, 2023
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
13 changes: 13 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 @@ -202,3 +202,16 @@ def g():

if sys.version_info >= (3,12):
print("py3")

# Slices on `sys.version_info` should be treated equivalently.
if sys.version_info[:2] >= (3,0):
print("py3")

if sys.version_info[:3] >= (3,0):
print("py3")

if sys.version_info[:2] > (3,13):
print("py3")

if sys.version_info[:3] > (3,13):
print("py3")
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::cmp::Ordering;
use anyhow::Result;
use ruff_diagnostics::{Diagnostic, Edit, Fix, FixAvailability, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::helpers::map_subscript;
use ruff_python_ast::stmt_if::{if_elif_branches, BranchKind, IfElifBranch};
use ruff_python_ast::whitespace::indentation;
use ruff_python_ast::{self as ast, CmpOp, Constant, ElifElseClause, Expr, Int, StmtIf};
Expand Down Expand Up @@ -94,9 +95,10 @@ pub(crate) fn outdated_version_block(checker: &mut Checker, stmt_if: &StmtIf) {
continue;
};

// Detect `sys.version_info`, along with slices (like `sys.version_info[:2]`).
if !checker
.semantic()
.resolve_call_path(left)
.resolve_call_path(map_subscript(left))
.is_some_and(|call_path| matches!(call_path.as_slice(), ["sys", "version_info"]))
{
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -729,5 +729,49 @@ UP036_0.py:203:4: UP036 [*] Version block is outdated for minimum Python version
203 |-if sys.version_info >= (3,12):
204 |- print("py3")
203 |+print("py3")
205 204 |
206 205 | # Slices on `sys.version_info` should be treated equivalently.
207 206 | if sys.version_info[:2] >= (3,0):

UP036_0.py:207:4: UP036 [*] Version block is outdated for minimum Python version
|
206 | # Slices on `sys.version_info` should be treated equivalently.
207 | if sys.version_info[:2] >= (3,0):
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ UP036
208 | print("py3")
|
= help: Remove outdated version block

ℹ Suggested fix
204 204 | print("py3")
205 205 |
206 206 | # Slices on `sys.version_info` should be treated equivalently.
207 |-if sys.version_info[:2] >= (3,0):
208 |- print("py3")
207 |+print("py3")
209 208 |
210 209 | if sys.version_info[:3] >= (3,0):
211 210 | print("py3")

UP036_0.py:210:4: UP036 [*] Version block is outdated for minimum Python version
|
208 | print("py3")
209 |
210 | if sys.version_info[:3] >= (3,0):
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ UP036
211 | print("py3")
|
= help: Remove outdated version block

ℹ Suggested fix
207 207 | if sys.version_info[:2] >= (3,0):
208 208 | print("py3")
209 209 |
210 |-if sys.version_info[:3] >= (3,0):
211 |- print("py3")
210 |+print("py3")
212 211 |
213 212 | if sys.version_info[:2] > (3,13):
214 213 | print("py3")


Loading