Skip to content

Commit 348815d

Browse files
committed
[flake8-pyi] Stabilize fix for unused-private-type-var (PYI018) (#16682)
## Summary This PR stabilizes the fix for `PYI018` introduced in #15999 (first released with Ruff 0.9.5 early February) There are no known issues with the fix or open PRs.
1 parent 1326d55 commit 348815d

6 files changed

+110
-222
lines changed

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,6 @@ mod tests {
174174
}
175175

176176
#[test_case(Rule::FutureAnnotationsInStub, Path::new("PYI044.pyi"))]
177-
#[test_case(Rule::UnusedPrivateTypeVar, Path::new("PYI018.py"))]
178-
#[test_case(Rule::UnusedPrivateTypeVar, Path::new("PYI018.pyi"))]
179177
fn preview_rules(rule_code: Rule, path: &Path) -> Result<()> {
180178
let snapshot = format!(
181179
"preview__{}_{}",

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,8 @@ use crate::fix;
2626
/// _Ts = typing_extensions.TypeVarTuple("_Ts")
2727
/// ```
2828
///
29-
/// ## Fix safety and availability
30-
/// This rule's fix is available when [`preview`] mode is enabled.
31-
/// It is always marked as unsafe, as it would break your code if the type
29+
/// ## Fix safety
30+
/// The fix is always marked as unsafe, as it would break your code if the type
3231
/// variable is imported by another module.
3332
#[derive(ViolationMetadata)]
3433
pub(crate) struct UnusedPrivateTypeVar {
@@ -225,18 +224,19 @@ pub(crate) fn unused_private_type_var(checker: &Checker, scope: &Scope) {
225224
continue;
226225
};
227226

228-
let mut diagnostic = Diagnostic::new(
227+
let diagnostic = Diagnostic::new(
229228
UnusedPrivateTypeVar {
230229
type_var_like_name: id.to_string(),
231230
type_var_like_kind: type_var_like_kind.to_string(),
232231
},
233232
binding.range(),
234-
);
235-
236-
if checker.settings.preview.is_enabled() {
237-
let edit = fix::edits::delete_stmt(stmt, None, checker.locator(), checker.indexer());
238-
diagnostic.set_fix(Fix::unsafe_edit(edit));
239-
}
233+
)
234+
.with_fix(Fix::unsafe_edit(fix::edits::delete_stmt(
235+
stmt,
236+
None,
237+
checker.locator(),
238+
checker.indexer(),
239+
)));
240240

241241
checker.report_diagnostic(diagnostic);
242242
}

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

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
source: crates/ruff_linter/src/rules/flake8_pyi/mod.rs
33
---
4-
PYI018.py:6:1: PYI018 Private TypeVar `_T` is never used
4+
PYI018.py:6:1: PYI018 [*] Private TypeVar `_T` is never used
55
|
66
4 | from typing_extensions import ParamSpec, TypeVarTuple
77
5 |
@@ -12,7 +12,16 @@ PYI018.py:6:1: PYI018 Private TypeVar `_T` is never used
1212
|
1313
= help: Remove unused private TypeVar `_T`
1414

15-
PYI018.py:7:1: PYI018 Private TypeVarTuple `_Ts` is never used
15+
Unsafe fix
16+
3 3 | from typing import TypeVar
17+
4 4 | from typing_extensions import ParamSpec, TypeVarTuple
18+
5 5 |
19+
6 |-_T = typing.TypeVar("_T")
20+
7 6 | _Ts = typing_extensions.TypeVarTuple("_Ts")
21+
8 7 | _P = ParamSpec("_P")
22+
9 8 | _P2 = typing.ParamSpec("_P2")
23+
24+
PYI018.py:7:1: PYI018 [*] Private TypeVarTuple `_Ts` is never used
1625
|
1726
6 | _T = typing.TypeVar("_T")
1827
7 | _Ts = typing_extensions.TypeVarTuple("_Ts")
@@ -22,7 +31,16 @@ PYI018.py:7:1: PYI018 Private TypeVarTuple `_Ts` is never used
2231
|
2332
= help: Remove unused private TypeVarTuple `_Ts`
2433

25-
PYI018.py:8:1: PYI018 Private ParamSpec `_P` is never used
34+
Unsafe fix
35+
4 4 | from typing_extensions import ParamSpec, TypeVarTuple
36+
5 5 |
37+
6 6 | _T = typing.TypeVar("_T")
38+
7 |-_Ts = typing_extensions.TypeVarTuple("_Ts")
39+
8 7 | _P = ParamSpec("_P")
40+
9 8 | _P2 = typing.ParamSpec("_P2")
41+
10 9 | _Ts2 = TypeVarTuple("_Ts2")
42+
43+
PYI018.py:8:1: PYI018 [*] Private ParamSpec `_P` is never used
2644
|
2745
6 | _T = typing.TypeVar("_T")
2846
7 | _Ts = typing_extensions.TypeVarTuple("_Ts")
@@ -33,7 +51,16 @@ PYI018.py:8:1: PYI018 Private ParamSpec `_P` is never used
3351
|
3452
= help: Remove unused private ParamSpec `_P`
3553

36-
PYI018.py:9:1: PYI018 Private ParamSpec `_P2` is never used
54+
Unsafe fix
55+
5 5 |
56+
6 6 | _T = typing.TypeVar("_T")
57+
7 7 | _Ts = typing_extensions.TypeVarTuple("_Ts")
58+
8 |-_P = ParamSpec("_P")
59+
9 8 | _P2 = typing.ParamSpec("_P2")
60+
10 9 | _Ts2 = TypeVarTuple("_Ts2")
61+
11 10 |
62+
63+
PYI018.py:9:1: PYI018 [*] Private ParamSpec `_P2` is never used
3764
|
3865
7 | _Ts = typing_extensions.TypeVarTuple("_Ts")
3966
8 | _P = ParamSpec("_P")
@@ -43,7 +70,16 @@ PYI018.py:9:1: PYI018 Private ParamSpec `_P2` is never used
4370
|
4471
= help: Remove unused private ParamSpec `_P2`
4572

46-
PYI018.py:10:1: PYI018 Private TypeVarTuple `_Ts2` is never used
73+
Unsafe fix
74+
6 6 | _T = typing.TypeVar("_T")
75+
7 7 | _Ts = typing_extensions.TypeVarTuple("_Ts")
76+
8 8 | _P = ParamSpec("_P")
77+
9 |-_P2 = typing.ParamSpec("_P2")
78+
10 9 | _Ts2 = TypeVarTuple("_Ts2")
79+
11 10 |
80+
12 11 | # OK
81+
82+
PYI018.py:10:1: PYI018 [*] Private TypeVarTuple `_Ts2` is never used
4783
|
4884
8 | _P = ParamSpec("_P")
4985
9 | _P2 = typing.ParamSpec("_P2")
@@ -53,3 +89,12 @@ PYI018.py:10:1: PYI018 Private TypeVarTuple `_Ts2` is never used
5389
12 | # OK
5490
|
5591
= help: Remove unused private TypeVarTuple `_Ts2`
92+
93+
Unsafe fix
94+
7 7 | _Ts = typing_extensions.TypeVarTuple("_Ts")
95+
8 8 | _P = ParamSpec("_P")
96+
9 9 | _P2 = typing.ParamSpec("_P2")
97+
10 |-_Ts2 = TypeVarTuple("_Ts2")
98+
11 10 |
99+
12 11 | # OK
100+
13 12 | _UsedTypeVar = TypeVar("_UsedTypeVar")

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

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
source: crates/ruff_linter/src/rules/flake8_pyi/mod.rs
33
---
4-
PYI018.pyi:6:1: PYI018 Private TypeVar `_T` is never used
4+
PYI018.pyi:6:1: PYI018 [*] Private TypeVar `_T` is never used
55
|
66
4 | from typing_extensions import ParamSpec, TypeVarTuple
77
5 |
@@ -12,7 +12,16 @@ PYI018.pyi:6:1: PYI018 Private TypeVar `_T` is never used
1212
|
1313
= help: Remove unused private TypeVar `_T`
1414

15-
PYI018.pyi:7:1: PYI018 Private TypeVarTuple `_Ts` is never used
15+
Unsafe fix
16+
3 3 | from typing import TypeVar
17+
4 4 | from typing_extensions import ParamSpec, TypeVarTuple
18+
5 5 |
19+
6 |-_T = typing.TypeVar("_T")
20+
7 6 | _Ts = typing_extensions.TypeVarTuple("_Ts")
21+
8 7 | _P = ParamSpec("_P")
22+
9 8 | _P2 = typing.ParamSpec("_P2")
23+
24+
PYI018.pyi:7:1: PYI018 [*] Private TypeVarTuple `_Ts` is never used
1625
|
1726
6 | _T = typing.TypeVar("_T")
1827
7 | _Ts = typing_extensions.TypeVarTuple("_Ts")
@@ -22,7 +31,16 @@ PYI018.pyi:7:1: PYI018 Private TypeVarTuple `_Ts` is never used
2231
|
2332
= help: Remove unused private TypeVarTuple `_Ts`
2433

25-
PYI018.pyi:8:1: PYI018 Private ParamSpec `_P` is never used
34+
Unsafe fix
35+
4 4 | from typing_extensions import ParamSpec, TypeVarTuple
36+
5 5 |
37+
6 6 | _T = typing.TypeVar("_T")
38+
7 |-_Ts = typing_extensions.TypeVarTuple("_Ts")
39+
8 7 | _P = ParamSpec("_P")
40+
9 8 | _P2 = typing.ParamSpec("_P2")
41+
10 9 | _Ts2 = TypeVarTuple("_Ts2")
42+
43+
PYI018.pyi:8:1: PYI018 [*] Private ParamSpec `_P` is never used
2644
|
2745
6 | _T = typing.TypeVar("_T")
2846
7 | _Ts = typing_extensions.TypeVarTuple("_Ts")
@@ -33,7 +51,16 @@ PYI018.pyi:8:1: PYI018 Private ParamSpec `_P` is never used
3351
|
3452
= help: Remove unused private ParamSpec `_P`
3553

36-
PYI018.pyi:9:1: PYI018 Private ParamSpec `_P2` is never used
54+
Unsafe fix
55+
5 5 |
56+
6 6 | _T = typing.TypeVar("_T")
57+
7 7 | _Ts = typing_extensions.TypeVarTuple("_Ts")
58+
8 |-_P = ParamSpec("_P")
59+
9 8 | _P2 = typing.ParamSpec("_P2")
60+
10 9 | _Ts2 = TypeVarTuple("_Ts2")
61+
11 10 |
62+
63+
PYI018.pyi:9:1: PYI018 [*] Private ParamSpec `_P2` is never used
3764
|
3865
7 | _Ts = typing_extensions.TypeVarTuple("_Ts")
3966
8 | _P = ParamSpec("_P")
@@ -43,7 +70,16 @@ PYI018.pyi:9:1: PYI018 Private ParamSpec `_P2` is never used
4370
|
4471
= help: Remove unused private ParamSpec `_P2`
4572

46-
PYI018.pyi:10:1: PYI018 Private TypeVarTuple `_Ts2` is never used
73+
Unsafe fix
74+
6 6 | _T = typing.TypeVar("_T")
75+
7 7 | _Ts = typing_extensions.TypeVarTuple("_Ts")
76+
8 8 | _P = ParamSpec("_P")
77+
9 |-_P2 = typing.ParamSpec("_P2")
78+
10 9 | _Ts2 = TypeVarTuple("_Ts2")
79+
11 10 |
80+
12 11 | # OK
81+
82+
PYI018.pyi:10:1: PYI018 [*] Private TypeVarTuple `_Ts2` is never used
4783
|
4884
8 | _P = ParamSpec("_P")
4985
9 | _P2 = typing.ParamSpec("_P2")
@@ -53,3 +89,12 @@ PYI018.pyi:10:1: PYI018 Private TypeVarTuple `_Ts2` is never used
5389
12 | # OK
5490
|
5591
= help: Remove unused private TypeVarTuple `_Ts2`
92+
93+
Unsafe fix
94+
7 7 | _Ts = typing_extensions.TypeVarTuple("_Ts")
95+
8 8 | _P = ParamSpec("_P")
96+
9 9 | _P2 = typing.ParamSpec("_P2")
97+
10 |-_Ts2 = TypeVarTuple("_Ts2")
98+
11 10 |
99+
12 11 | # OK
100+
13 12 | _UsedTypeVar = TypeVar("_UsedTypeVar")

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

Lines changed: 0 additions & 100 deletions
This file was deleted.

0 commit comments

Comments
 (0)