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

[flake8-pyi] Stabilize: include all python file types for PYI006 #15340

Merged
merged 2 commits into from
Jan 8, 2025
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
44 changes: 21 additions & 23 deletions crates/ruff_linter/src/checkers/ast/analyze/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1199,40 +1199,38 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
}
}
if checker.any_enabled(&[Rule::BadVersionInfoComparison, Rule::BadVersionInfoOrder]) {
if checker.source_type.is_stub() || checker.settings.preview.is_enabled() {
fn bad_version_info_comparison(
checker: &mut Checker,
test: &Expr,
has_else_clause: bool,
) {
if let Expr::BoolOp(ast::ExprBoolOp { values, .. }) = test {
for value in values {
flake8_pyi::rules::bad_version_info_comparison(
checker,
value,
has_else_clause,
);
}
} else {
fn bad_version_info_comparison(
Copy link
Member Author

Choose a reason for hiding this comment

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

The diff here looks misleading. All I did is to remove the outermost if

checker: &mut Checker,
test: &Expr,
has_else_clause: bool,
) {
if let Expr::BoolOp(ast::ExprBoolOp { values, .. }) = test {
for value in values {
flake8_pyi::rules::bad_version_info_comparison(
checker,
test,
value,
has_else_clause,
);
}
} else {
flake8_pyi::rules::bad_version_info_comparison(
checker,
test,
has_else_clause,
);
}
}

let has_else_clause =
elif_else_clauses.iter().any(|clause| clause.test.is_none());
let has_else_clause = elif_else_clauses.iter().any(|clause| clause.test.is_none());

bad_version_info_comparison(checker, test.as_ref(), has_else_clause);
for clause in elif_else_clauses {
if let Some(test) = clause.test.as_ref() {
bad_version_info_comparison(checker, test, has_else_clause);
}
bad_version_info_comparison(checker, test.as_ref(), has_else_clause);
for clause in elif_else_clauses {
if let Some(test) = clause.test.as_ref() {
bad_version_info_comparison(checker, test, has_else_clause);
}
}
}

if checker.enabled(Rule::IfKeyInDictDel) {
ruff::rules::if_key_in_dict_del(checker, if_);
}
Expand Down
2 changes: 0 additions & 2 deletions crates/ruff_linter/src/rules/flake8_pyi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,6 @@ mod tests {
}

#[test_case(Rule::FutureAnnotationsInStub, Path::new("PYI044.pyi"))]
#[test_case(Rule::BadVersionInfoComparison, Path::new("PYI006.py"))]
#[test_case(Rule::BadVersionInfoComparison, Path::new("PYI006.pyi"))]
fn preview_rules(rule_code: Rule, path: &Path) -> Result<()> {
let snapshot = format!(
"preview__{}_{}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::registry::Rule;

/// ## What it does
/// Checks for uses of comparators other than `<` and `>=` for
/// `sys.version_info` checks in `.pyi` files. All other comparators, such
/// `sys.version_info` checks. All other comparators, such
/// as `>`, `<=`, and `==`, are banned.
///
/// ## Why is this bad?
Expand All @@ -34,17 +34,15 @@ use crate::registry::Rule;
/// False
/// ```
///
/// In [preview], this rule will also flag non-stub files.
///
/// ## Example
/// ```pyi
/// ```py
/// import sys
///
/// if sys.version_info > (3, 8): ...
/// ```
///
/// Use instead:
/// ```pyi
/// ```py
/// import sys
///
/// if sys.version_info >= (3, 9): ...
Expand Down Expand Up @@ -144,7 +142,10 @@ pub(crate) fn bad_version_info_comparison(
}

if matches!(op, CmpOp::Lt) {
if checker.enabled(Rule::BadVersionInfoOrder) {
if checker.enabled(Rule::BadVersionInfoOrder)
// See https://github.com/astral-sh/ruff/issues/15347
&& (checker.source_type.is_stub() || checker.settings.preview.is_enabled())
{
if has_else_clause {
checker
.diagnostics
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,76 @@
source: crates/ruff_linter/src/rules/flake8_pyi/mod.rs
snapshot_kind: text
---
PYI006.py:8:4: PYI006 Use `<` or `>=` for `sys.version_info` comparisons
|
6 | if sys.version_info >= (3, 9): ... # OK
7 |
8 | if sys.version_info == (3, 9): ... # OK
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI006
9 |
10 | if sys.version_info == (3, 9): ... # Error: PYI006 Use only `<` and `>=` for version info comparisons
|

PYI006.py:10:4: PYI006 Use `<` or `>=` for `sys.version_info` comparisons
|
8 | if sys.version_info == (3, 9): ... # OK
9 |
10 | if sys.version_info == (3, 9): ... # Error: PYI006 Use only `<` and `>=` for version info comparisons
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI006
11 |
12 | if sys.version_info <= (3, 10): ... # Error: PYI006 Use only `<` and `>=` for version info comparisons
|

PYI006.py:12:4: PYI006 Use `<` or `>=` for `sys.version_info` comparisons
|
10 | if sys.version_info == (3, 9): ... # Error: PYI006 Use only `<` and `>=` for version info comparisons
11 |
12 | if sys.version_info <= (3, 10): ... # Error: PYI006 Use only `<` and `>=` for version info comparisons
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI006
13 |
14 | if sys.version_info <= (3, 10): ... # Error: PYI006 Use only `<` and `>=` for version info comparisons
|

PYI006.py:14:4: PYI006 Use `<` or `>=` for `sys.version_info` comparisons
|
12 | if sys.version_info <= (3, 10): ... # Error: PYI006 Use only `<` and `>=` for version info comparisons
13 |
14 | if sys.version_info <= (3, 10): ... # Error: PYI006 Use only `<` and `>=` for version info comparisons
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI006
15 |
16 | if sys.version_info > (3, 10): ... # Error: PYI006 Use only `<` and `>=` for version info comparisons
|

PYI006.py:16:4: PYI006 Use `<` or `>=` for `sys.version_info` comparisons
|
14 | if sys.version_info <= (3, 10): ... # Error: PYI006 Use only `<` and `>=` for version info comparisons
15 |
16 | if sys.version_info > (3, 10): ... # Error: PYI006 Use only `<` and `>=` for version info comparisons
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI006
17 | elif sys.version_info > (3, 11): ... # Error: PYI006 Use only `<` and `>=` for version info comparisons
|

PYI006.py:17:6: PYI006 Use `<` or `>=` for `sys.version_info` comparisons
|
16 | if sys.version_info > (3, 10): ... # Error: PYI006 Use only `<` and `>=` for version info comparisons
17 | elif sys.version_info > (3, 11): ... # Error: PYI006 Use only `<` and `>=` for version info comparisons
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI006
18 |
19 | if python_version > (3, 10): ... # Error: PYI006 Use only `<` and `>=` for version info comparisons
|

PYI006.py:19:4: PYI006 Use `<` or `>=` for `sys.version_info` comparisons
|
17 | elif sys.version_info > (3, 11): ... # Error: PYI006 Use only `<` and `>=` for version info comparisons
18 |
19 | if python_version > (3, 10): ... # Error: PYI006 Use only `<` and `>=` for version info comparisons
| ^^^^^^^^^^^^^^^^^^^^^^^^ PYI006
20 | elif python_version == (3, 11): ... # Error: PYI006 Use only `<` and `>=` for version info comparisons
|

PYI006.py:20:6: PYI006 Use `<` or `>=` for `sys.version_info` comparisons
|
19 | if python_version > (3, 10): ... # Error: PYI006 Use only `<` and `>=` for version info comparisons
20 | elif python_version == (3, 11): ... # Error: PYI006 Use only `<` and `>=` for version info comparisons
| ^^^^^^^^^^^^^^^^^^^^^^^^^ PYI006
|
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
source: crates/ruff_linter/src/rules/flake8_pyi/mod.rs
snapshot_kind: text
---
PYI066.pyi:3:4: PYI066 Put branches for newer Python versions first when branching on `sys.version_info` comparisons
|
Expand Down

This file was deleted.

This file was deleted.

Loading