From cdf38ca41920a8b75c3f6a19dd7cd1b8e1f9ca46 Mon Sep 17 00:00:00 2001 From: Micha Reiser Date: Wed, 12 Mar 2025 16:56:46 +0100 Subject: [PATCH] [`flake8-pyi`] Stabilize fix for `unused-private-type-var` (`PYI018`) --- .../ruff_linter/src/rules/flake8_pyi/mod.rs | 2 - .../rules/unused_private_type_definition.rs | 20 ++-- ...__flake8_pyi__tests__PYI018_PYI018.py.snap | 55 +++++++++- ..._flake8_pyi__tests__PYI018_PYI018.pyi.snap | 55 +++++++++- ...pyi__tests__preview__PYI018_PYI018.py.snap | 100 ------------------ ...yi__tests__preview__PYI018_PYI018.pyi.snap | 100 ------------------ 6 files changed, 110 insertions(+), 222 deletions(-) delete mode 100644 crates/ruff_linter/src/rules/flake8_pyi/snapshots/ruff_linter__rules__flake8_pyi__tests__preview__PYI018_PYI018.py.snap delete mode 100644 crates/ruff_linter/src/rules/flake8_pyi/snapshots/ruff_linter__rules__flake8_pyi__tests__preview__PYI018_PYI018.pyi.snap diff --git a/crates/ruff_linter/src/rules/flake8_pyi/mod.rs b/crates/ruff_linter/src/rules/flake8_pyi/mod.rs index af5ff7f7f1159..eef87b404aefc 100644 --- a/crates/ruff_linter/src/rules/flake8_pyi/mod.rs +++ b/crates/ruff_linter/src/rules/flake8_pyi/mod.rs @@ -174,8 +174,6 @@ mod tests { } #[test_case(Rule::FutureAnnotationsInStub, Path::new("PYI044.pyi"))] - #[test_case(Rule::UnusedPrivateTypeVar, Path::new("PYI018.py"))] - #[test_case(Rule::UnusedPrivateTypeVar, Path::new("PYI018.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/unused_private_type_definition.rs b/crates/ruff_linter/src/rules/flake8_pyi/rules/unused_private_type_definition.rs index 47575512308ee..4b99c3998d71f 100644 --- a/crates/ruff_linter/src/rules/flake8_pyi/rules/unused_private_type_definition.rs +++ b/crates/ruff_linter/src/rules/flake8_pyi/rules/unused_private_type_definition.rs @@ -26,9 +26,8 @@ use crate::fix; /// _Ts = typing_extensions.TypeVarTuple("_Ts") /// ``` /// -/// ## Fix safety and availability -/// This rule's fix is available when [`preview`] mode is enabled. -/// It is always marked as unsafe, as it would break your code if the type +/// ## Fix safety +/// The fix is always marked as unsafe, as it would break your code if the type /// variable is imported by another module. #[derive(ViolationMetadata)] pub(crate) struct UnusedPrivateTypeVar { @@ -225,18 +224,19 @@ pub(crate) fn unused_private_type_var(checker: &Checker, scope: &Scope) { continue; }; - let mut diagnostic = Diagnostic::new( + let diagnostic = Diagnostic::new( UnusedPrivateTypeVar { type_var_like_name: id.to_string(), type_var_like_kind: type_var_like_kind.to_string(), }, binding.range(), - ); - - if checker.settings.preview.is_enabled() { - let edit = fix::edits::delete_stmt(stmt, None, checker.locator(), checker.indexer()); - diagnostic.set_fix(Fix::unsafe_edit(edit)); - } + ) + .with_fix(Fix::unsafe_edit(fix::edits::delete_stmt( + stmt, + None, + checker.locator(), + checker.indexer(), + ))); checker.report_diagnostic(diagnostic); } diff --git a/crates/ruff_linter/src/rules/flake8_pyi/snapshots/ruff_linter__rules__flake8_pyi__tests__PYI018_PYI018.py.snap b/crates/ruff_linter/src/rules/flake8_pyi/snapshots/ruff_linter__rules__flake8_pyi__tests__PYI018_PYI018.py.snap index 3d8c84bc13690..19ab98788364a 100644 --- a/crates/ruff_linter/src/rules/flake8_pyi/snapshots/ruff_linter__rules__flake8_pyi__tests__PYI018_PYI018.py.snap +++ b/crates/ruff_linter/src/rules/flake8_pyi/snapshots/ruff_linter__rules__flake8_pyi__tests__PYI018_PYI018.py.snap @@ -1,7 +1,7 @@ --- source: crates/ruff_linter/src/rules/flake8_pyi/mod.rs --- -PYI018.py:6:1: PYI018 Private TypeVar `_T` is never used +PYI018.py:6:1: PYI018 [*] Private TypeVar `_T` is never used | 4 | from typing_extensions import ParamSpec, TypeVarTuple 5 | @@ -12,7 +12,16 @@ PYI018.py:6:1: PYI018 Private TypeVar `_T` is never used | = help: Remove unused private TypeVar `_T` -PYI018.py:7:1: PYI018 Private TypeVarTuple `_Ts` is never used +ℹ Unsafe fix +3 3 | from typing import TypeVar +4 4 | from typing_extensions import ParamSpec, TypeVarTuple +5 5 | +6 |-_T = typing.TypeVar("_T") +7 6 | _Ts = typing_extensions.TypeVarTuple("_Ts") +8 7 | _P = ParamSpec("_P") +9 8 | _P2 = typing.ParamSpec("_P2") + +PYI018.py:7:1: PYI018 [*] Private TypeVarTuple `_Ts` is never used | 6 | _T = typing.TypeVar("_T") 7 | _Ts = typing_extensions.TypeVarTuple("_Ts") @@ -22,7 +31,16 @@ PYI018.py:7:1: PYI018 Private TypeVarTuple `_Ts` is never used | = help: Remove unused private TypeVarTuple `_Ts` -PYI018.py:8:1: PYI018 Private ParamSpec `_P` is never used +ℹ Unsafe fix +4 4 | from typing_extensions import ParamSpec, TypeVarTuple +5 5 | +6 6 | _T = typing.TypeVar("_T") +7 |-_Ts = typing_extensions.TypeVarTuple("_Ts") +8 7 | _P = ParamSpec("_P") +9 8 | _P2 = typing.ParamSpec("_P2") +10 9 | _Ts2 = TypeVarTuple("_Ts2") + +PYI018.py:8:1: PYI018 [*] Private ParamSpec `_P` is never used | 6 | _T = typing.TypeVar("_T") 7 | _Ts = typing_extensions.TypeVarTuple("_Ts") @@ -33,7 +51,16 @@ PYI018.py:8:1: PYI018 Private ParamSpec `_P` is never used | = help: Remove unused private ParamSpec `_P` -PYI018.py:9:1: PYI018 Private ParamSpec `_P2` is never used +ℹ Unsafe fix +5 5 | +6 6 | _T = typing.TypeVar("_T") +7 7 | _Ts = typing_extensions.TypeVarTuple("_Ts") +8 |-_P = ParamSpec("_P") +9 8 | _P2 = typing.ParamSpec("_P2") +10 9 | _Ts2 = TypeVarTuple("_Ts2") +11 10 | + +PYI018.py:9:1: PYI018 [*] Private ParamSpec `_P2` is never used | 7 | _Ts = typing_extensions.TypeVarTuple("_Ts") 8 | _P = ParamSpec("_P") @@ -43,7 +70,16 @@ PYI018.py:9:1: PYI018 Private ParamSpec `_P2` is never used | = help: Remove unused private ParamSpec `_P2` -PYI018.py:10:1: PYI018 Private TypeVarTuple `_Ts2` is never used +ℹ Unsafe fix +6 6 | _T = typing.TypeVar("_T") +7 7 | _Ts = typing_extensions.TypeVarTuple("_Ts") +8 8 | _P = ParamSpec("_P") +9 |-_P2 = typing.ParamSpec("_P2") +10 9 | _Ts2 = TypeVarTuple("_Ts2") +11 10 | +12 11 | # OK + +PYI018.py:10:1: PYI018 [*] Private TypeVarTuple `_Ts2` is never used | 8 | _P = ParamSpec("_P") 9 | _P2 = typing.ParamSpec("_P2") @@ -53,3 +89,12 @@ PYI018.py:10:1: PYI018 Private TypeVarTuple `_Ts2` is never used 12 | # OK | = help: Remove unused private TypeVarTuple `_Ts2` + +ℹ Unsafe fix +7 7 | _Ts = typing_extensions.TypeVarTuple("_Ts") +8 8 | _P = ParamSpec("_P") +9 9 | _P2 = typing.ParamSpec("_P2") +10 |-_Ts2 = TypeVarTuple("_Ts2") +11 10 | +12 11 | # OK +13 12 | _UsedTypeVar = TypeVar("_UsedTypeVar") diff --git a/crates/ruff_linter/src/rules/flake8_pyi/snapshots/ruff_linter__rules__flake8_pyi__tests__PYI018_PYI018.pyi.snap b/crates/ruff_linter/src/rules/flake8_pyi/snapshots/ruff_linter__rules__flake8_pyi__tests__PYI018_PYI018.pyi.snap index 4661096325aa8..a7913fbfa888e 100644 --- a/crates/ruff_linter/src/rules/flake8_pyi/snapshots/ruff_linter__rules__flake8_pyi__tests__PYI018_PYI018.pyi.snap +++ b/crates/ruff_linter/src/rules/flake8_pyi/snapshots/ruff_linter__rules__flake8_pyi__tests__PYI018_PYI018.pyi.snap @@ -1,7 +1,7 @@ --- source: crates/ruff_linter/src/rules/flake8_pyi/mod.rs --- -PYI018.pyi:6:1: PYI018 Private TypeVar `_T` is never used +PYI018.pyi:6:1: PYI018 [*] Private TypeVar `_T` is never used | 4 | from typing_extensions import ParamSpec, TypeVarTuple 5 | @@ -12,7 +12,16 @@ PYI018.pyi:6:1: PYI018 Private TypeVar `_T` is never used | = help: Remove unused private TypeVar `_T` -PYI018.pyi:7:1: PYI018 Private TypeVarTuple `_Ts` is never used +ℹ Unsafe fix +3 3 | from typing import TypeVar +4 4 | from typing_extensions import ParamSpec, TypeVarTuple +5 5 | +6 |-_T = typing.TypeVar("_T") +7 6 | _Ts = typing_extensions.TypeVarTuple("_Ts") +8 7 | _P = ParamSpec("_P") +9 8 | _P2 = typing.ParamSpec("_P2") + +PYI018.pyi:7:1: PYI018 [*] Private TypeVarTuple `_Ts` is never used | 6 | _T = typing.TypeVar("_T") 7 | _Ts = typing_extensions.TypeVarTuple("_Ts") @@ -22,7 +31,16 @@ PYI018.pyi:7:1: PYI018 Private TypeVarTuple `_Ts` is never used | = help: Remove unused private TypeVarTuple `_Ts` -PYI018.pyi:8:1: PYI018 Private ParamSpec `_P` is never used +ℹ Unsafe fix +4 4 | from typing_extensions import ParamSpec, TypeVarTuple +5 5 | +6 6 | _T = typing.TypeVar("_T") +7 |-_Ts = typing_extensions.TypeVarTuple("_Ts") +8 7 | _P = ParamSpec("_P") +9 8 | _P2 = typing.ParamSpec("_P2") +10 9 | _Ts2 = TypeVarTuple("_Ts2") + +PYI018.pyi:8:1: PYI018 [*] Private ParamSpec `_P` is never used | 6 | _T = typing.TypeVar("_T") 7 | _Ts = typing_extensions.TypeVarTuple("_Ts") @@ -33,7 +51,16 @@ PYI018.pyi:8:1: PYI018 Private ParamSpec `_P` is never used | = help: Remove unused private ParamSpec `_P` -PYI018.pyi:9:1: PYI018 Private ParamSpec `_P2` is never used +ℹ Unsafe fix +5 5 | +6 6 | _T = typing.TypeVar("_T") +7 7 | _Ts = typing_extensions.TypeVarTuple("_Ts") +8 |-_P = ParamSpec("_P") +9 8 | _P2 = typing.ParamSpec("_P2") +10 9 | _Ts2 = TypeVarTuple("_Ts2") +11 10 | + +PYI018.pyi:9:1: PYI018 [*] Private ParamSpec `_P2` is never used | 7 | _Ts = typing_extensions.TypeVarTuple("_Ts") 8 | _P = ParamSpec("_P") @@ -43,7 +70,16 @@ PYI018.pyi:9:1: PYI018 Private ParamSpec `_P2` is never used | = help: Remove unused private ParamSpec `_P2` -PYI018.pyi:10:1: PYI018 Private TypeVarTuple `_Ts2` is never used +ℹ Unsafe fix +6 6 | _T = typing.TypeVar("_T") +7 7 | _Ts = typing_extensions.TypeVarTuple("_Ts") +8 8 | _P = ParamSpec("_P") +9 |-_P2 = typing.ParamSpec("_P2") +10 9 | _Ts2 = TypeVarTuple("_Ts2") +11 10 | +12 11 | # OK + +PYI018.pyi:10:1: PYI018 [*] Private TypeVarTuple `_Ts2` is never used | 8 | _P = ParamSpec("_P") 9 | _P2 = typing.ParamSpec("_P2") @@ -53,3 +89,12 @@ PYI018.pyi:10:1: PYI018 Private TypeVarTuple `_Ts2` is never used 12 | # OK | = help: Remove unused private TypeVarTuple `_Ts2` + +ℹ Unsafe fix +7 7 | _Ts = typing_extensions.TypeVarTuple("_Ts") +8 8 | _P = ParamSpec("_P") +9 9 | _P2 = typing.ParamSpec("_P2") +10 |-_Ts2 = TypeVarTuple("_Ts2") +11 10 | +12 11 | # OK +13 12 | _UsedTypeVar = TypeVar("_UsedTypeVar") diff --git a/crates/ruff_linter/src/rules/flake8_pyi/snapshots/ruff_linter__rules__flake8_pyi__tests__preview__PYI018_PYI018.py.snap b/crates/ruff_linter/src/rules/flake8_pyi/snapshots/ruff_linter__rules__flake8_pyi__tests__preview__PYI018_PYI018.py.snap deleted file mode 100644 index 19ab98788364a..0000000000000 --- a/crates/ruff_linter/src/rules/flake8_pyi/snapshots/ruff_linter__rules__flake8_pyi__tests__preview__PYI018_PYI018.py.snap +++ /dev/null @@ -1,100 +0,0 @@ ---- -source: crates/ruff_linter/src/rules/flake8_pyi/mod.rs ---- -PYI018.py:6:1: PYI018 [*] Private TypeVar `_T` is never used - | -4 | from typing_extensions import ParamSpec, TypeVarTuple -5 | -6 | _T = typing.TypeVar("_T") - | ^^ PYI018 -7 | _Ts = typing_extensions.TypeVarTuple("_Ts") -8 | _P = ParamSpec("_P") - | - = help: Remove unused private TypeVar `_T` - -ℹ Unsafe fix -3 3 | from typing import TypeVar -4 4 | from typing_extensions import ParamSpec, TypeVarTuple -5 5 | -6 |-_T = typing.TypeVar("_T") -7 6 | _Ts = typing_extensions.TypeVarTuple("_Ts") -8 7 | _P = ParamSpec("_P") -9 8 | _P2 = typing.ParamSpec("_P2") - -PYI018.py:7:1: PYI018 [*] Private TypeVarTuple `_Ts` is never used - | -6 | _T = typing.TypeVar("_T") -7 | _Ts = typing_extensions.TypeVarTuple("_Ts") - | ^^^ PYI018 -8 | _P = ParamSpec("_P") -9 | _P2 = typing.ParamSpec("_P2") - | - = help: Remove unused private TypeVarTuple `_Ts` - -ℹ Unsafe fix -4 4 | from typing_extensions import ParamSpec, TypeVarTuple -5 5 | -6 6 | _T = typing.TypeVar("_T") -7 |-_Ts = typing_extensions.TypeVarTuple("_Ts") -8 7 | _P = ParamSpec("_P") -9 8 | _P2 = typing.ParamSpec("_P2") -10 9 | _Ts2 = TypeVarTuple("_Ts2") - -PYI018.py:8:1: PYI018 [*] Private ParamSpec `_P` is never used - | - 6 | _T = typing.TypeVar("_T") - 7 | _Ts = typing_extensions.TypeVarTuple("_Ts") - 8 | _P = ParamSpec("_P") - | ^^ PYI018 - 9 | _P2 = typing.ParamSpec("_P2") -10 | _Ts2 = TypeVarTuple("_Ts2") - | - = help: Remove unused private ParamSpec `_P` - -ℹ Unsafe fix -5 5 | -6 6 | _T = typing.TypeVar("_T") -7 7 | _Ts = typing_extensions.TypeVarTuple("_Ts") -8 |-_P = ParamSpec("_P") -9 8 | _P2 = typing.ParamSpec("_P2") -10 9 | _Ts2 = TypeVarTuple("_Ts2") -11 10 | - -PYI018.py:9:1: PYI018 [*] Private ParamSpec `_P2` is never used - | - 7 | _Ts = typing_extensions.TypeVarTuple("_Ts") - 8 | _P = ParamSpec("_P") - 9 | _P2 = typing.ParamSpec("_P2") - | ^^^ PYI018 -10 | _Ts2 = TypeVarTuple("_Ts2") - | - = help: Remove unused private ParamSpec `_P2` - -ℹ Unsafe fix -6 6 | _T = typing.TypeVar("_T") -7 7 | _Ts = typing_extensions.TypeVarTuple("_Ts") -8 8 | _P = ParamSpec("_P") -9 |-_P2 = typing.ParamSpec("_P2") -10 9 | _Ts2 = TypeVarTuple("_Ts2") -11 10 | -12 11 | # OK - -PYI018.py:10:1: PYI018 [*] Private TypeVarTuple `_Ts2` is never used - | - 8 | _P = ParamSpec("_P") - 9 | _P2 = typing.ParamSpec("_P2") -10 | _Ts2 = TypeVarTuple("_Ts2") - | ^^^^ PYI018 -11 | -12 | # OK - | - = help: Remove unused private TypeVarTuple `_Ts2` - -ℹ Unsafe fix -7 7 | _Ts = typing_extensions.TypeVarTuple("_Ts") -8 8 | _P = ParamSpec("_P") -9 9 | _P2 = typing.ParamSpec("_P2") -10 |-_Ts2 = TypeVarTuple("_Ts2") -11 10 | -12 11 | # OK -13 12 | _UsedTypeVar = TypeVar("_UsedTypeVar") diff --git a/crates/ruff_linter/src/rules/flake8_pyi/snapshots/ruff_linter__rules__flake8_pyi__tests__preview__PYI018_PYI018.pyi.snap b/crates/ruff_linter/src/rules/flake8_pyi/snapshots/ruff_linter__rules__flake8_pyi__tests__preview__PYI018_PYI018.pyi.snap deleted file mode 100644 index a7913fbfa888e..0000000000000 --- a/crates/ruff_linter/src/rules/flake8_pyi/snapshots/ruff_linter__rules__flake8_pyi__tests__preview__PYI018_PYI018.pyi.snap +++ /dev/null @@ -1,100 +0,0 @@ ---- -source: crates/ruff_linter/src/rules/flake8_pyi/mod.rs ---- -PYI018.pyi:6:1: PYI018 [*] Private TypeVar `_T` is never used - | -4 | from typing_extensions import ParamSpec, TypeVarTuple -5 | -6 | _T = typing.TypeVar("_T") - | ^^ PYI018 -7 | _Ts = typing_extensions.TypeVarTuple("_Ts") -8 | _P = ParamSpec("_P") - | - = help: Remove unused private TypeVar `_T` - -ℹ Unsafe fix -3 3 | from typing import TypeVar -4 4 | from typing_extensions import ParamSpec, TypeVarTuple -5 5 | -6 |-_T = typing.TypeVar("_T") -7 6 | _Ts = typing_extensions.TypeVarTuple("_Ts") -8 7 | _P = ParamSpec("_P") -9 8 | _P2 = typing.ParamSpec("_P2") - -PYI018.pyi:7:1: PYI018 [*] Private TypeVarTuple `_Ts` is never used - | -6 | _T = typing.TypeVar("_T") -7 | _Ts = typing_extensions.TypeVarTuple("_Ts") - | ^^^ PYI018 -8 | _P = ParamSpec("_P") -9 | _P2 = typing.ParamSpec("_P2") - | - = help: Remove unused private TypeVarTuple `_Ts` - -ℹ Unsafe fix -4 4 | from typing_extensions import ParamSpec, TypeVarTuple -5 5 | -6 6 | _T = typing.TypeVar("_T") -7 |-_Ts = typing_extensions.TypeVarTuple("_Ts") -8 7 | _P = ParamSpec("_P") -9 8 | _P2 = typing.ParamSpec("_P2") -10 9 | _Ts2 = TypeVarTuple("_Ts2") - -PYI018.pyi:8:1: PYI018 [*] Private ParamSpec `_P` is never used - | - 6 | _T = typing.TypeVar("_T") - 7 | _Ts = typing_extensions.TypeVarTuple("_Ts") - 8 | _P = ParamSpec("_P") - | ^^ PYI018 - 9 | _P2 = typing.ParamSpec("_P2") -10 | _Ts2 = TypeVarTuple("_Ts2") - | - = help: Remove unused private ParamSpec `_P` - -ℹ Unsafe fix -5 5 | -6 6 | _T = typing.TypeVar("_T") -7 7 | _Ts = typing_extensions.TypeVarTuple("_Ts") -8 |-_P = ParamSpec("_P") -9 8 | _P2 = typing.ParamSpec("_P2") -10 9 | _Ts2 = TypeVarTuple("_Ts2") -11 10 | - -PYI018.pyi:9:1: PYI018 [*] Private ParamSpec `_P2` is never used - | - 7 | _Ts = typing_extensions.TypeVarTuple("_Ts") - 8 | _P = ParamSpec("_P") - 9 | _P2 = typing.ParamSpec("_P2") - | ^^^ PYI018 -10 | _Ts2 = TypeVarTuple("_Ts2") - | - = help: Remove unused private ParamSpec `_P2` - -ℹ Unsafe fix -6 6 | _T = typing.TypeVar("_T") -7 7 | _Ts = typing_extensions.TypeVarTuple("_Ts") -8 8 | _P = ParamSpec("_P") -9 |-_P2 = typing.ParamSpec("_P2") -10 9 | _Ts2 = TypeVarTuple("_Ts2") -11 10 | -12 11 | # OK - -PYI018.pyi:10:1: PYI018 [*] Private TypeVarTuple `_Ts2` is never used - | - 8 | _P = ParamSpec("_P") - 9 | _P2 = typing.ParamSpec("_P2") -10 | _Ts2 = TypeVarTuple("_Ts2") - | ^^^^ PYI018 -11 | -12 | # OK - | - = help: Remove unused private TypeVarTuple `_Ts2` - -ℹ Unsafe fix -7 7 | _Ts = typing_extensions.TypeVarTuple("_Ts") -8 8 | _P = ParamSpec("_P") -9 9 | _P2 = typing.ParamSpec("_P2") -10 |-_Ts2 = TypeVarTuple("_Ts2") -11 10 | -12 11 | # OK -13 12 | _UsedTypeVar = TypeVar("_UsedTypeVar")