Skip to content

Commit d615fa7

Browse files
authored
Stabilize adding future import via config option (#20277)
Introduced in #19100. Removed gating, updated tests, removed warning(s), and updated documentation.
1 parent 3ec3847 commit d615fa7

File tree

13 files changed

+28
-79
lines changed

13 files changed

+28
-79
lines changed

crates/ruff/tests/lint.rs

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5780,28 +5780,6 @@ match 42: # invalid-syntax
57805780
Ok(())
57815781
}
57825782

5783-
#[test]
5784-
fn future_annotations_preview_warning() {
5785-
assert_cmd_snapshot!(
5786-
Command::new(get_cargo_bin(BIN_NAME))
5787-
.args(STDIN_BASE_OPTIONS)
5788-
.args(["--config", "lint.future-annotations = true"])
5789-
.args(["--select", "F"])
5790-
.arg("--no-preview")
5791-
.arg("-")
5792-
.pass_stdin("1"),
5793-
@r"
5794-
success: true
5795-
exit_code: 0
5796-
----- stdout -----
5797-
All checks passed!
5798-
5799-
----- stderr -----
5800-
warning: The `lint.future-annotations` setting will have no effect because `preview` is disabled
5801-
",
5802-
);
5803-
}
5804-
58055783
#[test]
58065784
fn up045_nested_optional_flatten_all() {
58075785
let contents = "\

crates/ruff_linter/src/preview.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -207,11 +207,6 @@ pub(crate) const fn is_safe_super_call_with_parameters_fix_enabled(
207207
settings.preview.is_enabled()
208208
}
209209

210-
// https://github.com/astral-sh/ruff/pull/19100
211-
pub(crate) const fn is_add_future_annotations_imports_enabled(settings: &LinterSettings) -> bool {
212-
settings.preview.is_enabled()
213-
}
214-
215210
// https://github.com/astral-sh/ruff/pull/19851
216211
pub(crate) const fn is_maxsplit_without_separator_fix_enabled(settings: &LinterSettings) -> bool {
217212
settings.preview.is_enabled()

crates/ruff_linter/src/rules/flake8_type_checking/helpers.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ impl TypingReference {
6666
}
6767

6868
// prefer `from __future__ import annotations` to quoting
69-
if settings.future_annotations()
69+
if settings.future_annotations
7070
&& !reference.in_typing_only_annotation()
7171
&& reference.in_runtime_evaluated_annotation()
7272
{

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ mod tests {
1414
use test_case::test_case;
1515

1616
use crate::registry::{Linter, Rule};
17-
use crate::settings::types::PreviewMode;
1817
use crate::test::{test_path, test_snippet};
1918
use crate::{assert_diagnostics, settings};
2019

@@ -86,7 +85,6 @@ mod tests {
8685
Path::new("flake8_type_checking").join(path).as_path(),
8786
&settings::LinterSettings {
8887
future_annotations: true,
89-
preview: PreviewMode::Enabled,
9088
// also enable quoting annotations to check the interaction. the future import
9189
// should take precedence.
9290
flake8_type_checking: super::settings::Settings {

crates/ruff_linter/src/rules/flake8_type_checking/rules/typing_only_runtime_import.rs

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ use crate::{Fix, FixAvailability, Violation};
3838
/// [`lint.flake8-type-checking.runtime-evaluated-decorators`] settings to mark them
3939
/// as such.
4040
///
41+
/// If [`lint.future-annotations`] is set to `true`, `from __future__ import
42+
/// annotations` will be added if doing so would enable an import to be
43+
/// moved into an `if TYPE_CHECKING:` block. This takes precedence over the
44+
/// [`lint.flake8-type-checking.quote-annotations`] setting described above if
45+
/// both settings are enabled.
46+
///
47+
///
4148
/// ## Example
4249
/// ```python
4350
/// from __future__ import annotations
@@ -63,14 +70,6 @@ use crate::{Fix, FixAvailability, Violation};
6370
/// return len(sized)
6471
/// ```
6572
///
66-
///
67-
/// ## Preview
68-
/// If [`lint.future-annotations`] is set to `true`, `from __future__ import
69-
/// annotations` will be added if doing so would enable an import to be moved into an `if
70-
/// TYPE_CHECKING:` block. This takes precedence over the
71-
/// [`lint.flake8-type-checking.quote-annotations`] setting described above if both settings are
72-
/// enabled.
73-
///
7473
/// ## Options
7574
/// - `lint.flake8-type-checking.quote-annotations`
7675
/// - `lint.flake8-type-checking.runtime-evaluated-base-classes`
@@ -122,6 +121,12 @@ impl Violation for TypingOnlyFirstPartyImport {
122121
/// [`lint.flake8-type-checking.runtime-evaluated-decorators`] settings to mark them
123122
/// as such.
124123
///
124+
/// If [`lint.future-annotations`] is set to `true`, `from __future__ import
125+
/// annotations` will be added if doing so would enable an import to be
126+
/// moved into an `if TYPE_CHECKING:` block. This takes precedence over the
127+
/// [`lint.flake8-type-checking.quote-annotations`] setting described above if
128+
/// both settings are enabled.
129+
///
125130
/// ## Example
126131
/// ```python
127132
/// from __future__ import annotations
@@ -147,13 +152,6 @@ impl Violation for TypingOnlyFirstPartyImport {
147152
/// return len(df)
148153
/// ```
149154
///
150-
/// ## Preview
151-
/// If [`lint.future-annotations`] is set to `true`, `from __future__ import
152-
/// annotations` will be added if doing so would enable an import to be moved into an `if
153-
/// TYPE_CHECKING:` block. This takes precedence over the
154-
/// [`lint.flake8-type-checking.quote-annotations`] setting described above if both settings are
155-
/// enabled.
156-
///
157155
/// ## Options
158156
/// - `lint.flake8-type-checking.quote-annotations`
159157
/// - `lint.flake8-type-checking.runtime-evaluated-base-classes`
@@ -205,6 +203,12 @@ impl Violation for TypingOnlyThirdPartyImport {
205203
/// [`lint.flake8-type-checking.runtime-evaluated-decorators`] settings to mark them
206204
/// as such.
207205
///
206+
/// If [`lint.future-annotations`] is set to `true`, `from __future__ import
207+
/// annotations` will be added if doing so would enable an import to be
208+
/// moved into an `if TYPE_CHECKING:` block. This takes precedence over the
209+
/// [`lint.flake8-type-checking.quote-annotations`] setting described above if
210+
/// both settings are enabled.
211+
///
208212
/// ## Example
209213
/// ```python
210214
/// from __future__ import annotations
@@ -230,15 +234,6 @@ impl Violation for TypingOnlyThirdPartyImport {
230234
/// return str(path)
231235
/// ```
232236
///
233-
/// ## Preview
234-
///
235-
/// When [preview](https://docs.astral.sh/ruff/preview/) is enabled, if
236-
/// [`lint.future-annotations`] is set to `true`, `from __future__ import
237-
/// annotations` will be added if doing so would enable an import to be moved into an `if
238-
/// TYPE_CHECKING:` block. This takes precedence over the
239-
/// [`lint.flake8-type-checking.quote-annotations`] setting described above if both settings are
240-
/// enabled.
241-
///
242237
/// ## Options
243238
/// - `lint.flake8-type-checking.quote-annotations`
244239
/// - `lint.flake8-type-checking.runtime-evaluated-base-classes`
@@ -287,7 +282,7 @@ pub(crate) fn typing_only_runtime_import(
287282

288283
// If we can't add a `__future__` import and in un-strict mode, don't flag typing-only
289284
// imports that are implicitly loaded by way of a valid runtime import.
290-
if !checker.settings().future_annotations()
285+
if !checker.settings().future_annotations
291286
&& !checker.settings().flake8_type_checking.strict
292287
&& runtime_imports
293288
.iter()

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,6 @@ mod tests {
147147
let diagnostics = test_path(
148148
Path::new("pyupgrade").join(path).as_path(),
149149
&settings::LinterSettings {
150-
preview: PreviewMode::Enabled,
151150
future_annotations: true,
152151
..settings::LinterSettings::for_rule(rule_code)
153152
},

crates/ruff_linter/src/rules/pyupgrade/rules/quoted_annotation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ impl AlwaysFixableViolation for QuotedAnnotation {
102102

103103
/// UP037
104104
pub(crate) fn quoted_annotation(checker: &Checker, annotation: &str, range: TextRange) {
105-
let add_future_import = checker.settings().future_annotations()
105+
let add_future_import = checker.settings().future_annotations
106106
&& checker.semantic().in_runtime_evaluated_annotation();
107107

108108
if !(checker.semantic().in_typing_only_annotation() || add_future_import) {

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,6 @@ mod tests {
649649
let diagnostics = test_path(
650650
Path::new("ruff").join(path).as_path(),
651651
&settings::LinterSettings {
652-
preview: PreviewMode::Enabled,
653652
future_annotations: true,
654653
unresolved_target_version: PythonVersion::PY39.into(),
655654
..settings::LinterSettings::for_rule(rule_code)

crates/ruff_linter/src/rules/ruff/rules/implicit_optional.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ use crate::rules::ruff::typing::type_hint_explicitly_allows_none;
1919
/// Checks for the use of implicit `Optional` in type annotations when the
2020
/// default parameter value is `None`.
2121
///
22+
/// If [`lint.future-annotations`] is set to `true`, `from __future__ import
23+
/// annotations` will be added if doing so would allow using the `|` operator on
24+
/// a Python version before 3.10.
25+
///
2226
/// ## Why is this bad?
2327
/// Implicit `Optional` is prohibited by [PEP 484]. It is confusing and
2428
/// inconsistent with the rest of the type system.
@@ -73,12 +77,6 @@ use crate::rules::ruff::typing::type_hint_explicitly_allows_none;
7377
/// - `target-version`
7478
/// - `lint.future-annotations`
7579
///
76-
/// ## Preview
77-
///
78-
/// When [preview] is enabled, if [`lint.future-annotations`] is set to `true`,
79-
/// `from __future__ import annotations` will be added if doing so would allow using the `|`
80-
/// operator on a Python version before 3.10.
81-
///
8280
/// ## Fix safety
8381
///
8482
/// This fix is always marked as unsafe because it can change the behavior of code that relies on
@@ -217,7 +215,7 @@ pub(crate) fn implicit_optional(checker: &Checker, parameters: &Parameters) {
217215
};
218216

219217
let conversion_type = if checker.target_version() >= PythonVersion::PY310
220-
|| checker.settings().future_annotations()
218+
|| checker.settings().future_annotations
221219
{
222220
ConversionType::BinOpOr
223221
} else {

crates/ruff_linter/src/settings/mod.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -475,11 +475,6 @@ impl LinterSettings {
475475
.is_match(path)
476476
.map_or(self.unresolved_target_version, TargetVersion::from)
477477
}
478-
479-
pub fn future_annotations(&self) -> bool {
480-
// TODO(brent) we can just access the field directly once this is stabilized.
481-
self.future_annotations && crate::preview::is_add_future_annotations_imports_enabled(self)
482-
}
483478
}
484479

485480
impl Default for LinterSettings {

0 commit comments

Comments
 (0)