Skip to content

Commit 3ea4c63

Browse files
committed
[flake8-pyi] Stabilize: include all python file types for PYI006 (#15340)
1 parent 8e8a071 commit 3ea4c63

7 files changed

+101
-185
lines changed

crates/ruff_linter/src/checkers/ast/analyze/statement.rs

+21-23
Original file line numberDiff line numberDiff line change
@@ -1199,40 +1199,38 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
11991199
}
12001200
}
12011201
if checker.any_enabled(&[Rule::BadVersionInfoComparison, Rule::BadVersionInfoOrder]) {
1202-
if checker.source_type.is_stub() || checker.settings.preview.is_enabled() {
1203-
fn bad_version_info_comparison(
1204-
checker: &mut Checker,
1205-
test: &Expr,
1206-
has_else_clause: bool,
1207-
) {
1208-
if let Expr::BoolOp(ast::ExprBoolOp { values, .. }) = test {
1209-
for value in values {
1210-
flake8_pyi::rules::bad_version_info_comparison(
1211-
checker,
1212-
value,
1213-
has_else_clause,
1214-
);
1215-
}
1216-
} else {
1202+
fn bad_version_info_comparison(
1203+
checker: &mut Checker,
1204+
test: &Expr,
1205+
has_else_clause: bool,
1206+
) {
1207+
if let Expr::BoolOp(ast::ExprBoolOp { values, .. }) = test {
1208+
for value in values {
12171209
flake8_pyi::rules::bad_version_info_comparison(
12181210
checker,
1219-
test,
1211+
value,
12201212
has_else_clause,
12211213
);
12221214
}
1215+
} else {
1216+
flake8_pyi::rules::bad_version_info_comparison(
1217+
checker,
1218+
test,
1219+
has_else_clause,
1220+
);
12231221
}
1222+
}
12241223

1225-
let has_else_clause =
1226-
elif_else_clauses.iter().any(|clause| clause.test.is_none());
1224+
let has_else_clause = elif_else_clauses.iter().any(|clause| clause.test.is_none());
12271225

1228-
bad_version_info_comparison(checker, test.as_ref(), has_else_clause);
1229-
for clause in elif_else_clauses {
1230-
if let Some(test) = clause.test.as_ref() {
1231-
bad_version_info_comparison(checker, test, has_else_clause);
1232-
}
1226+
bad_version_info_comparison(checker, test.as_ref(), has_else_clause);
1227+
for clause in elif_else_clauses {
1228+
if let Some(test) = clause.test.as_ref() {
1229+
bad_version_info_comparison(checker, test, has_else_clause);
12331230
}
12341231
}
12351232
}
1233+
12361234
if checker.enabled(Rule::IfKeyInDictDel) {
12371235
ruff::rules::if_key_in_dict_del(checker, if_);
12381236
}

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

-2
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,6 @@ mod tests {
187187
}
188188

189189
#[test_case(Rule::FutureAnnotationsInStub, Path::new("PYI044.pyi"))]
190-
#[test_case(Rule::BadVersionInfoComparison, Path::new("PYI006.py"))]
191-
#[test_case(Rule::BadVersionInfoComparison, Path::new("PYI006.pyi"))]
192190
fn preview_rules(rule_code: Rule, path: &Path) -> Result<()> {
193191
let snapshot = format!(
194192
"preview__{}_{}",

crates/ruff_linter/src/rules/flake8_pyi/rules/bad_version_info_comparison.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::registry::Rule;
99

1010
/// ## What it does
1111
/// Checks for uses of comparators other than `<` and `>=` for
12-
/// `sys.version_info` checks in `.pyi` files. All other comparators, such
12+
/// `sys.version_info` checks. All other comparators, such
1313
/// as `>`, `<=`, and `==`, are banned.
1414
///
1515
/// ## Why is this bad?
@@ -34,17 +34,15 @@ use crate::registry::Rule;
3434
/// False
3535
/// ```
3636
///
37-
/// In [preview], this rule will also flag non-stub files.
38-
///
3937
/// ## Example
40-
/// ```pyi
38+
/// ```py
4139
/// import sys
4240
///
4341
/// if sys.version_info > (3, 8): ...
4442
/// ```
4543
///
4644
/// Use instead:
47-
/// ```pyi
45+
/// ```py
4846
/// import sys
4947
///
5048
/// if sys.version_info >= (3, 9): ...
@@ -144,7 +142,10 @@ pub(crate) fn bad_version_info_comparison(
144142
}
145143

146144
if matches!(op, CmpOp::Lt) {
147-
if checker.enabled(Rule::BadVersionInfoOrder) {
145+
if checker.enabled(Rule::BadVersionInfoOrder)
146+
// See https://github.com/astral-sh/ruff/issues/15347
147+
&& (checker.source_type.is_stub() || checker.settings.preview.is_enabled())
148+
{
148149
if has_else_clause {
149150
checker
150151
.diagnostics

crates/ruff_linter/src/rules/flake8_pyi/snapshots/ruff_linter__rules__flake8_pyi__tests__PYI006_PYI006.py.snap

+72
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,76 @@
22
source: crates/ruff_linter/src/rules/flake8_pyi/mod.rs
33
snapshot_kind: text
44
---
5+
PYI006.py:8:4: PYI006 Use `<` or `>=` for `sys.version_info` comparisons
6+
|
7+
6 | if sys.version_info >= (3, 9): ... # OK
8+
7 |
9+
8 | if sys.version_info == (3, 9): ... # OK
10+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI006
11+
9 |
12+
10 | if sys.version_info == (3, 9): ... # Error: PYI006 Use only `<` and `>=` for version info comparisons
13+
|
514

15+
PYI006.py:10:4: PYI006 Use `<` or `>=` for `sys.version_info` comparisons
16+
|
17+
8 | if sys.version_info == (3, 9): ... # OK
18+
9 |
19+
10 | if sys.version_info == (3, 9): ... # Error: PYI006 Use only `<` and `>=` for version info comparisons
20+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI006
21+
11 |
22+
12 | if sys.version_info <= (3, 10): ... # Error: PYI006 Use only `<` and `>=` for version info comparisons
23+
|
24+
25+
PYI006.py:12:4: PYI006 Use `<` or `>=` for `sys.version_info` comparisons
26+
|
27+
10 | if sys.version_info == (3, 9): ... # Error: PYI006 Use only `<` and `>=` for version info comparisons
28+
11 |
29+
12 | if sys.version_info <= (3, 10): ... # Error: PYI006 Use only `<` and `>=` for version info comparisons
30+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI006
31+
13 |
32+
14 | if sys.version_info <= (3, 10): ... # Error: PYI006 Use only `<` and `>=` for version info comparisons
33+
|
34+
35+
PYI006.py:14:4: PYI006 Use `<` or `>=` for `sys.version_info` comparisons
36+
|
37+
12 | if sys.version_info <= (3, 10): ... # Error: PYI006 Use only `<` and `>=` for version info comparisons
38+
13 |
39+
14 | if sys.version_info <= (3, 10): ... # Error: PYI006 Use only `<` and `>=` for version info comparisons
40+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI006
41+
15 |
42+
16 | if sys.version_info > (3, 10): ... # Error: PYI006 Use only `<` and `>=` for version info comparisons
43+
|
44+
45+
PYI006.py:16:4: PYI006 Use `<` or `>=` for `sys.version_info` comparisons
46+
|
47+
14 | if sys.version_info <= (3, 10): ... # Error: PYI006 Use only `<` and `>=` for version info comparisons
48+
15 |
49+
16 | if sys.version_info > (3, 10): ... # Error: PYI006 Use only `<` and `>=` for version info comparisons
50+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI006
51+
17 | elif sys.version_info > (3, 11): ... # Error: PYI006 Use only `<` and `>=` for version info comparisons
52+
|
53+
54+
PYI006.py:17:6: PYI006 Use `<` or `>=` for `sys.version_info` comparisons
55+
|
56+
16 | if sys.version_info > (3, 10): ... # Error: PYI006 Use only `<` and `>=` for version info comparisons
57+
17 | elif sys.version_info > (3, 11): ... # Error: PYI006 Use only `<` and `>=` for version info comparisons
58+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI006
59+
18 |
60+
19 | if python_version > (3, 10): ... # Error: PYI006 Use only `<` and `>=` for version info comparisons
61+
|
62+
63+
PYI006.py:19:4: PYI006 Use `<` or `>=` for `sys.version_info` comparisons
64+
|
65+
17 | elif sys.version_info > (3, 11): ... # Error: PYI006 Use only `<` and `>=` for version info comparisons
66+
18 |
67+
19 | if python_version > (3, 10): ... # Error: PYI006 Use only `<` and `>=` for version info comparisons
68+
| ^^^^^^^^^^^^^^^^^^^^^^^^ PYI006
69+
20 | elif python_version == (3, 11): ... # Error: PYI006 Use only `<` and `>=` for version info comparisons
70+
|
71+
72+
PYI006.py:20:6: PYI006 Use `<` or `>=` for `sys.version_info` comparisons
73+
|
74+
19 | if python_version > (3, 10): ... # Error: PYI006 Use only `<` and `>=` for version info comparisons
75+
20 | elif python_version == (3, 11): ... # Error: PYI006 Use only `<` and `>=` for version info comparisons
76+
| ^^^^^^^^^^^^^^^^^^^^^^^^^ PYI006
77+
|

crates/ruff_linter/src/rules/flake8_pyi/snapshots/ruff_linter__rules__flake8_pyi__tests__PYI066_PYI066.pyi.snap

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
---
22
source: crates/ruff_linter/src/rules/flake8_pyi/mod.rs
3+
snapshot_kind: text
34
---
45
PYI066.pyi:3:4: PYI066 Put branches for newer Python versions first when branching on `sys.version_info` comparisons
56
|

crates/ruff_linter/src/rules/flake8_pyi/snapshots/ruff_linter__rules__flake8_pyi__tests__preview__PYI006_PYI006.py.snap

-77
This file was deleted.

crates/ruff_linter/src/rules/flake8_pyi/snapshots/ruff_linter__rules__flake8_pyi__tests__preview__PYI006_PYI006.pyi.snap

-77
This file was deleted.

0 commit comments

Comments
 (0)