From 1d66c9f5739daf3c51e732cab20ac372bd1f2ac2 Mon Sep 17 00:00:00 2001 From: Micha Reiser Date: Wed, 8 Jan 2025 14:08:33 +0100 Subject: [PATCH] [`flake8-pyi`] Stabilize: include all python file types for `PYI006` (#15340) --- .../src/checkers/ast/analyze/statement.rs | 44 +++++------ .../ruff_linter/src/rules/flake8_pyi/mod.rs | 2 - .../rules/bad_version_info_comparison.rs | 13 ++-- ...__flake8_pyi__tests__PYI006_PYI006.py.snap | 72 +++++++++++++++++ ..._flake8_pyi__tests__PYI066_PYI066.pyi.snap | 1 + ...pyi__tests__preview__PYI006_PYI006.py.snap | 77 ------------------- ...yi__tests__preview__PYI006_PYI006.pyi.snap | 77 ------------------- 7 files changed, 101 insertions(+), 185 deletions(-) delete mode 100644 crates/ruff_linter/src/rules/flake8_pyi/snapshots/ruff_linter__rules__flake8_pyi__tests__preview__PYI006_PYI006.py.snap delete mode 100644 crates/ruff_linter/src/rules/flake8_pyi/snapshots/ruff_linter__rules__flake8_pyi__tests__preview__PYI006_PYI006.pyi.snap diff --git a/crates/ruff_linter/src/checkers/ast/analyze/statement.rs b/crates/ruff_linter/src/checkers/ast/analyze/statement.rs index e55de3831c597a..b1671c612559d6 100644 --- a/crates/ruff_linter/src/checkers/ast/analyze/statement.rs +++ b/crates/ruff_linter/src/checkers/ast/analyze/statement.rs @@ -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( + 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_); } diff --git a/crates/ruff_linter/src/rules/flake8_pyi/mod.rs b/crates/ruff_linter/src/rules/flake8_pyi/mod.rs index 13c2fa2745e16c..b06c5cef0e0e70 100644 --- a/crates/ruff_linter/src/rules/flake8_pyi/mod.rs +++ b/crates/ruff_linter/src/rules/flake8_pyi/mod.rs @@ -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__{}_{}", diff --git a/crates/ruff_linter/src/rules/flake8_pyi/rules/bad_version_info_comparison.rs b/crates/ruff_linter/src/rules/flake8_pyi/rules/bad_version_info_comparison.rs index b606470452c2c8..4b0954fa3c0394 100644 --- a/crates/ruff_linter/src/rules/flake8_pyi/rules/bad_version_info_comparison.rs +++ b/crates/ruff_linter/src/rules/flake8_pyi/rules/bad_version_info_comparison.rs @@ -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? @@ -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): ... @@ -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 diff --git a/crates/ruff_linter/src/rules/flake8_pyi/snapshots/ruff_linter__rules__flake8_pyi__tests__PYI006_PYI006.py.snap b/crates/ruff_linter/src/rules/flake8_pyi/snapshots/ruff_linter__rules__flake8_pyi__tests__PYI006_PYI006.py.snap index 5990d00bfebec8..a5f79c0998a3cb 100644 --- a/crates/ruff_linter/src/rules/flake8_pyi/snapshots/ruff_linter__rules__flake8_pyi__tests__PYI006_PYI006.py.snap +++ b/crates/ruff_linter/src/rules/flake8_pyi/snapshots/ruff_linter__rules__flake8_pyi__tests__PYI006_PYI006.py.snap @@ -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 + | diff --git a/crates/ruff_linter/src/rules/flake8_pyi/snapshots/ruff_linter__rules__flake8_pyi__tests__PYI066_PYI066.pyi.snap b/crates/ruff_linter/src/rules/flake8_pyi/snapshots/ruff_linter__rules__flake8_pyi__tests__PYI066_PYI066.pyi.snap index dd98502eebd164..706fc9d07dbe88 100644 --- a/crates/ruff_linter/src/rules/flake8_pyi/snapshots/ruff_linter__rules__flake8_pyi__tests__PYI066_PYI066.pyi.snap +++ b/crates/ruff_linter/src/rules/flake8_pyi/snapshots/ruff_linter__rules__flake8_pyi__tests__PYI066_PYI066.pyi.snap @@ -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 | diff --git a/crates/ruff_linter/src/rules/flake8_pyi/snapshots/ruff_linter__rules__flake8_pyi__tests__preview__PYI006_PYI006.py.snap b/crates/ruff_linter/src/rules/flake8_pyi/snapshots/ruff_linter__rules__flake8_pyi__tests__preview__PYI006_PYI006.py.snap deleted file mode 100644 index a5f79c0998a3cb..00000000000000 --- a/crates/ruff_linter/src/rules/flake8_pyi/snapshots/ruff_linter__rules__flake8_pyi__tests__preview__PYI006_PYI006.py.snap +++ /dev/null @@ -1,77 +0,0 @@ ---- -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 - | diff --git a/crates/ruff_linter/src/rules/flake8_pyi/snapshots/ruff_linter__rules__flake8_pyi__tests__preview__PYI006_PYI006.pyi.snap b/crates/ruff_linter/src/rules/flake8_pyi/snapshots/ruff_linter__rules__flake8_pyi__tests__preview__PYI006_PYI006.pyi.snap deleted file mode 100644 index 2ff61307f9468e..00000000000000 --- a/crates/ruff_linter/src/rules/flake8_pyi/snapshots/ruff_linter__rules__flake8_pyi__tests__preview__PYI006_PYI006.pyi.snap +++ /dev/null @@ -1,77 +0,0 @@ ---- -source: crates/ruff_linter/src/rules/flake8_pyi/mod.rs -snapshot_kind: text ---- -PYI006.pyi: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.pyi: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.pyi: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.pyi: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.pyi: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.pyi: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.pyi: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.pyi: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 - |