From 50550c8c35a40ea5f503d2b3f9aef3e69a8c4137 Mon Sep 17 00:00:00 2001 From: Micha Reiser Date: Wed, 8 Jan 2025 10:10:18 +0100 Subject: [PATCH 1/2] [`flake8-pyi`] Stabilize: include all python file types for `PYI006` and `PYI066` --- .../src/checkers/ast/analyze/statement.rs | 44 +++++------ .../ruff_linter/src/rules/flake8_pyi/mod.rs | 2 - .../rules/bad_version_info_comparison.rs | 15 ++-- ...__flake8_pyi__tests__PYI006_PYI006.py.snap | 72 +++++++++++++++++ ...__flake8_pyi__tests__PYI066_PYI066.py.snap | 48 ++++++++++++ ...pyi__tests__preview__PYI006_PYI006.py.snap | 77 ------------------- ...yi__tests__preview__PYI006_PYI006.pyi.snap | 77 ------------------- 7 files changed, 146 insertions(+), 189 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..69c30bd7ce8403 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): ... @@ -74,12 +72,9 @@ impl Violation for BadVersionInfoComparison { /// This rule enforces the convention by checking for `if` tests that compare /// `sys.version_info` with `<` rather than `>=`. /// -/// By default, this rule only applies to stub files. -/// In [preview], it will also flag this anti-pattern in non-stub files. -/// /// ## Example /// -/// ```pyi +/// ```py /// import sys /// /// if sys.version_info < (3, 10): @@ -91,7 +86,7 @@ impl Violation for BadVersionInfoComparison { /// /// Use instead: /// -/// ```pyi +/// ```py /// if sys.version_info >= (3, 10): /// def read_data(x): ... /// 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.py.snap b/crates/ruff_linter/src/rules/flake8_pyi/snapshots/ruff_linter__rules__flake8_pyi__tests__PYI066_PYI066.py.snap index 5990d00bfebec8..420b92ffd8cd6c 100644 --- a/crates/ruff_linter/src/rules/flake8_pyi/snapshots/ruff_linter__rules__flake8_pyi__tests__PYI066_PYI066.py.snap +++ b/crates/ruff_linter/src/rules/flake8_pyi/snapshots/ruff_linter__rules__flake8_pyi__tests__PYI066_PYI066.py.snap @@ -2,4 +2,52 @@ source: crates/ruff_linter/src/rules/flake8_pyi/mod.rs snapshot_kind: text --- +PYI066.py:3:4: PYI066 Put branches for newer Python versions first when branching on `sys.version_info` comparisons + | +1 | import sys +2 | +3 | if sys.version_info < (3, 10): # Y066 When using if/else with sys.version_info, put the code for new Python versions first, e.g. "if sys.version_info >= (3, 10)" + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI066 +4 | def foo(x): ... +5 | else: + | +PYI066.py:8:4: PYI066 Put branches for newer Python versions first when branching on `sys.version_info` comparisons + | + 6 | def foo(x, *, bar=True): ... + 7 | + 8 | if sys.version_info < (3, 8): # Y066 When using if/else with sys.version_info, put the code for new Python versions first, e.g. "if sys.version_info >= (3, 8)" + | ^^^^^^^^^^^^^^^^^^^^^^^^^ PYI066 + 9 | def bar(x): ... +10 | elif sys.version_info < (3, 9): # Y066 When using if/else with sys.version_info, put the code for new Python versions first, e.g. "if sys.version_info >= (3, 9)" + | + +PYI066.py:10:6: PYI066 Put branches for newer Python versions first when branching on `sys.version_info` comparisons + | + 8 | if sys.version_info < (3, 8): # Y066 When using if/else with sys.version_info, put the code for new Python versions first, e.g. "if sys.version_info >= (3, 8)" + 9 | def bar(x): ... +10 | elif sys.version_info < (3, 9): # Y066 When using if/else with sys.version_info, put the code for new Python versions first, e.g. "if sys.version_info >= (3, 9)" + | ^^^^^^^^^^^^^^^^^^^^^^^^^ PYI066 +11 | def bar(x, *, bar=True): ... +12 | elif sys.version_info < (3, 11): # Y066 When using if/else with sys.version_info, put the code for new Python versions first, e.g. "if sys.version_info >= (3, 10)" + | + +PYI066.py:12:6: PYI066 Put branches for newer Python versions first when branching on `sys.version_info` comparisons + | +10 | elif sys.version_info < (3, 9): # Y066 When using if/else with sys.version_info, put the code for new Python versions first, e.g. "if sys.version_info >= (3, 9)" +11 | def bar(x, *, bar=True): ... +12 | elif sys.version_info < (3, 11): # Y066 When using if/else with sys.version_info, put the code for new Python versions first, e.g. "if sys.version_info >= (3, 10)" + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI066 +13 | def bar(x, *, bar=True, baz=False): ... +14 | else: + | + +PYI066.py:20:6: PYI066 Put branches for newer Python versions first when branching on `sys.version_info` comparisons + | +18 | if sys.version_info >= (3, 5): +19 | ... +20 | elif sys.version_info < (3, 9): # Y066 When using if/else with sys.version_info, put the code for new Python versions first, e.g. "if sys.version_info >= (3, 10)" + | ^^^^^^^^^^^^^^^^^^^^^^^^^ PYI066 +21 | ... +22 | else: + | 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 - | From d04358399676d57ae49c99cadf69614e23e3435a Mon Sep 17 00:00:00 2001 From: Micha Reiser Date: Wed, 8 Jan 2025 13:31:18 +0100 Subject: [PATCH 2/2] Revert PYI066 changes --- .../rules/bad_version_info_comparison.rs | 12 +++-- ...__flake8_pyi__tests__PYI066_PYI066.py.snap | 48 ------------------- ..._flake8_pyi__tests__PYI066_PYI066.pyi.snap | 1 + 3 files changed, 10 insertions(+), 51 deletions(-) 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 69c30bd7ce8403..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 @@ -72,9 +72,12 @@ impl Violation for BadVersionInfoComparison { /// This rule enforces the convention by checking for `if` tests that compare /// `sys.version_info` with `<` rather than `>=`. /// +/// By default, this rule only applies to stub files. +/// In [preview], it will also flag this anti-pattern in non-stub files. +/// /// ## Example /// -/// ```py +/// ```pyi /// import sys /// /// if sys.version_info < (3, 10): @@ -86,7 +89,7 @@ impl Violation for BadVersionInfoComparison { /// /// Use instead: /// -/// ```py +/// ```pyi /// if sys.version_info >= (3, 10): /// def read_data(x): ... /// @@ -139,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__PYI066_PYI066.py.snap b/crates/ruff_linter/src/rules/flake8_pyi/snapshots/ruff_linter__rules__flake8_pyi__tests__PYI066_PYI066.py.snap index 420b92ffd8cd6c..5990d00bfebec8 100644 --- a/crates/ruff_linter/src/rules/flake8_pyi/snapshots/ruff_linter__rules__flake8_pyi__tests__PYI066_PYI066.py.snap +++ b/crates/ruff_linter/src/rules/flake8_pyi/snapshots/ruff_linter__rules__flake8_pyi__tests__PYI066_PYI066.py.snap @@ -2,52 +2,4 @@ source: crates/ruff_linter/src/rules/flake8_pyi/mod.rs snapshot_kind: text --- -PYI066.py:3:4: PYI066 Put branches for newer Python versions first when branching on `sys.version_info` comparisons - | -1 | import sys -2 | -3 | if sys.version_info < (3, 10): # Y066 When using if/else with sys.version_info, put the code for new Python versions first, e.g. "if sys.version_info >= (3, 10)" - | ^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI066 -4 | def foo(x): ... -5 | else: - | -PYI066.py:8:4: PYI066 Put branches for newer Python versions first when branching on `sys.version_info` comparisons - | - 6 | def foo(x, *, bar=True): ... - 7 | - 8 | if sys.version_info < (3, 8): # Y066 When using if/else with sys.version_info, put the code for new Python versions first, e.g. "if sys.version_info >= (3, 8)" - | ^^^^^^^^^^^^^^^^^^^^^^^^^ PYI066 - 9 | def bar(x): ... -10 | elif sys.version_info < (3, 9): # Y066 When using if/else with sys.version_info, put the code for new Python versions first, e.g. "if sys.version_info >= (3, 9)" - | - -PYI066.py:10:6: PYI066 Put branches for newer Python versions first when branching on `sys.version_info` comparisons - | - 8 | if sys.version_info < (3, 8): # Y066 When using if/else with sys.version_info, put the code for new Python versions first, e.g. "if sys.version_info >= (3, 8)" - 9 | def bar(x): ... -10 | elif sys.version_info < (3, 9): # Y066 When using if/else with sys.version_info, put the code for new Python versions first, e.g. "if sys.version_info >= (3, 9)" - | ^^^^^^^^^^^^^^^^^^^^^^^^^ PYI066 -11 | def bar(x, *, bar=True): ... -12 | elif sys.version_info < (3, 11): # Y066 When using if/else with sys.version_info, put the code for new Python versions first, e.g. "if sys.version_info >= (3, 10)" - | - -PYI066.py:12:6: PYI066 Put branches for newer Python versions first when branching on `sys.version_info` comparisons - | -10 | elif sys.version_info < (3, 9): # Y066 When using if/else with sys.version_info, put the code for new Python versions first, e.g. "if sys.version_info >= (3, 9)" -11 | def bar(x, *, bar=True): ... -12 | elif sys.version_info < (3, 11): # Y066 When using if/else with sys.version_info, put the code for new Python versions first, e.g. "if sys.version_info >= (3, 10)" - | ^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI066 -13 | def bar(x, *, bar=True, baz=False): ... -14 | else: - | - -PYI066.py:20:6: PYI066 Put branches for newer Python versions first when branching on `sys.version_info` comparisons - | -18 | if sys.version_info >= (3, 5): -19 | ... -20 | elif sys.version_info < (3, 9): # Y066 When using if/else with sys.version_info, put the code for new Python versions first, e.g. "if sys.version_info >= (3, 10)" - | ^^^^^^^^^^^^^^^^^^^^^^^^^ PYI066 -21 | ... -22 | else: - | 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 |