Skip to content

Commit ec7fe41

Browse files
dylwil3ntBre
authored andcommitted
[flake8-pyi] Stabilize autofix for future-annotations-in-stub (PYI044) (#18518)
1 parent 5b834f2 commit ec7fe41

File tree

5 files changed

+31
-79
lines changed

5 files changed

+31
-79
lines changed

crates/ruff_linter/src/preview.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,6 @@ pub(crate) const fn is_bad_version_info_in_non_stub_enabled(settings: &LinterSet
5252
settings.preview.is_enabled()
5353
}
5454

55-
// https://github.com/astral-sh/ruff/pull/12676
56-
pub(crate) const fn is_fix_future_annotations_in_stub_enabled(settings: &LinterSettings) -> bool {
57-
settings.preview.is_enabled()
58-
}
59-
6055
// https://github.com/astral-sh/ruff/pull/11074
6156
pub(crate) const fn is_only_add_return_none_at_end_enabled(settings: &LinterSettings) -> bool {
6257
settings.preview.is_enabled()

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

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ mod tests {
1111

1212
use crate::registry::Rule;
1313
use crate::rules::pep8_naming;
14-
use crate::settings::types::PreviewMode;
1514
use crate::test::test_path;
1615
use crate::{assert_messages, settings};
1716

@@ -172,22 +171,4 @@ mod tests {
172171
assert_messages!(snapshot, diagnostics);
173172
Ok(())
174173
}
175-
176-
#[test_case(Rule::FutureAnnotationsInStub, Path::new("PYI044.pyi"))]
177-
fn preview_rules(rule_code: Rule, path: &Path) -> Result<()> {
178-
let snapshot = format!(
179-
"preview__{}_{}",
180-
rule_code.noqa_code(),
181-
path.to_string_lossy()
182-
);
183-
let diagnostics = test_path(
184-
Path::new("flake8_pyi").join(path).as_path(),
185-
&settings::LinterSettings {
186-
preview: PreviewMode::Enabled,
187-
..settings::LinterSettings::for_rule(rule_code)
188-
},
189-
)?;
190-
assert_messages!(snapshot, diagnostics);
191-
Ok(())
192-
}
193174
}

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

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use ruff_python_ast::StmtImportFrom;
33
use ruff_macros::{ViolationMetadata, derive_message_formats};
44

55
use crate::{Fix, FixAvailability, Violation};
6-
use crate::{checkers::ast::Checker, fix, preview::is_fix_future_annotations_in_stub_enabled};
6+
use crate::{checkers::ast::Checker, fix};
77

88
/// ## What it does
99
/// Checks for the presence of the `from __future__ import annotations` import
@@ -55,20 +55,18 @@ pub(crate) fn from_future_import(checker: &Checker, target: &StmtImportFrom) {
5555

5656
let mut diagnostic = checker.report_diagnostic(FutureAnnotationsInStub, *range);
5757

58-
if is_fix_future_annotations_in_stub_enabled(checker.settings) {
59-
let stmt = checker.semantic().current_statement();
58+
let stmt = checker.semantic().current_statement();
6059

61-
diagnostic.try_set_fix(|| {
62-
let edit = fix::edits::remove_unused_imports(
63-
std::iter::once("annotations"),
64-
stmt,
65-
None,
66-
checker.locator(),
67-
checker.stylist(),
68-
checker.indexer(),
69-
)?;
60+
diagnostic.try_set_fix(|| {
61+
let edit = fix::edits::remove_unused_imports(
62+
std::iter::once("annotations"),
63+
stmt,
64+
None,
65+
checker.locator(),
66+
checker.stylist(),
67+
checker.indexer(),
68+
)?;
7069

71-
Ok(Fix::safe_edit(edit))
72-
});
73-
}
70+
Ok(Fix::safe_edit(edit))
71+
});
7472
}

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

Lines changed: 18 additions & 2 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-
PYI044.pyi:2:1: PYI044 `from __future__ import annotations` has no effect in stub files, since type checkers automatically treat stubs as having those semantics
4+
PYI044.pyi:2:1: PYI044 [*] `from __future__ import annotations` has no effect in stub files, since type checkers automatically treat stubs as having those semantics
55
|
66
1 | # Bad import.
77
2 | from __future__ import annotations # PYI044.
@@ -10,7 +10,14 @@ PYI044.pyi:2:1: PYI044 `from __future__ import annotations` has no effect in stu
1010
|
1111
= help: Remove `from __future__ import annotations`
1212

13-
PYI044.pyi:3:1: PYI044 `from __future__ import annotations` has no effect in stub files, since type checkers automatically treat stubs as having those semantics
13+
Safe fix
14+
1 1 | # Bad import.
15+
2 |-from __future__ import annotations # PYI044.
16+
3 2 | from __future__ import annotations, with_statement # PYI044.
17+
4 3 |
18+
5 4 | # Good imports.
19+
20+
PYI044.pyi:3:1: PYI044 [*] `from __future__ import annotations` has no effect in stub files, since type checkers automatically treat stubs as having those semantics
1421
|
1522
1 | # Bad import.
1623
2 | from __future__ import annotations # PYI044.
@@ -20,3 +27,12 @@ PYI044.pyi:3:1: PYI044 `from __future__ import annotations` has no effect in stu
2027
5 | # Good imports.
2128
|
2229
= help: Remove `from __future__ import annotations`
30+
31+
Safe fix
32+
1 1 | # Bad import.
33+
2 2 | from __future__ import annotations # PYI044.
34+
3 |-from __future__ import annotations, with_statement # PYI044.
35+
3 |+from __future__ import with_statement # PYI044.
36+
4 4 |
37+
5 5 | # Good imports.
38+
6 6 | from __future__ import with_statement

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

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

0 commit comments

Comments
 (0)