From 3526092b00793d4735a531940d9c008d6caca50c Mon Sep 17 00:00:00 2001 From: Zanie Blue Date: Tue, 12 Mar 2024 12:29:11 -0500 Subject: [PATCH] Deprecate F401 `ignore-init-module-imports` and make default behavior --- .../show_settings__display_default_settings.snap | 2 +- .../src/rules/pyflakes/rules/unused_import.rs | 6 +++--- crates/ruff_workspace/src/configuration.rs | 10 ++++++++-- crates/ruff_workspace/src/options.rs | 9 +++++++-- ruff.schema.json | 5 +++-- 5 files changed, 22 insertions(+), 10 deletions(-) diff --git a/crates/ruff/tests/snapshots/show_settings__display_default_settings.snap b/crates/ruff/tests/snapshots/show_settings__display_default_settings.snap index 6b7d064333d183..2298b64ea1e337 100644 --- a/crates/ruff/tests/snapshots/show_settings__display_default_settings.snap +++ b/crates/ruff/tests/snapshots/show_settings__display_default_settings.snap @@ -201,7 +201,7 @@ linter.allowed_confusables = [] linter.builtins = [] linter.dummy_variable_rgx = ^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$ linter.external = [] -linter.ignore_init_module_imports = false +linter.ignore_init_module_imports = true linter.logger_objects = [] linter.namespace_packages = [] linter.src = [ diff --git a/crates/ruff_linter/src/rules/pyflakes/rules/unused_import.rs b/crates/ruff_linter/src/rules/pyflakes/rules/unused_import.rs index 1499ad779128eb..a65d8bfe423648 100644 --- a/crates/ruff_linter/src/rules/pyflakes/rules/unused_import.rs +++ b/crates/ruff_linter/src/rules/pyflakes/rules/unused_import.rs @@ -154,8 +154,8 @@ pub(crate) fn unused_import(checker: &Checker, scope: &Scope, diagnostics: &mut } } - let in_init = - checker.settings.ignore_init_module_imports && checker.path().ends_with("__init__.py"); + let in_init = checker.path().ends_with("__init__.py"); + let fix_init = in_init && !checker.settings.ignore_init_module_imports; // Generate a diagnostic for every import, but share a fix across all imports within the same // statement (excluding those that are ignored). @@ -164,7 +164,7 @@ pub(crate) fn unused_import(checker: &Checker, scope: &Scope, diagnostics: &mut exceptions.intersects(Exceptions::MODULE_NOT_FOUND_ERROR | Exceptions::IMPORT_ERROR); let multiple = imports.len() > 1; - let fix = if !in_init && !in_except_handler { + let fix = if !fix_init && !in_except_handler { fix_imports(checker, node_id, &imports).ok() } else { None diff --git a/crates/ruff_workspace/src/configuration.rs b/crates/ruff_workspace/src/configuration.rs index e6dfea2f37d833..5414515e3d7956 100644 --- a/crates/ruff_workspace/src/configuration.rs +++ b/crates/ruff_workspace/src/configuration.rs @@ -237,6 +237,7 @@ impl Configuration { project_root: project_root.to_path_buf(), }, + #[allow(deprecated)] linter: LinterSettings { rules: lint.as_rule_table(lint_preview)?, exclude: FilePatternSet::try_from_iter(lint.exclude.unwrap_or_default())?, @@ -253,7 +254,7 @@ impl Configuration { .dummy_variable_rgx .unwrap_or_else(|| DUMMY_VARIABLE_RGX.clone()), external: lint.external.unwrap_or_default(), - ignore_init_module_imports: lint.ignore_init_module_imports.unwrap_or_default(), + ignore_init_module_imports: lint.ignore_init_module_imports.unwrap_or(true), line_length, tab_size: self.indent_width.unwrap_or_default(), namespace_packages: self.namespace_packages.unwrap_or_default(), @@ -650,6 +651,10 @@ impl LintConfiguration { .flatten() .chain(options.common.extend_unfixable.into_iter().flatten()) .collect(); + + #[allow(deprecated)] + let ignore_init_module_imports = options.common.ignore_init_module_imports; + Ok(LintConfiguration { exclude: options.exclude.map(|paths| { paths @@ -692,7 +697,7 @@ impl LintConfiguration { }) .unwrap_or_default(), external: options.common.external, - ignore_init_module_imports: options.common.ignore_init_module_imports, + ignore_init_module_imports, explicit_preview_rules: options.common.explicit_preview_rules, per_file_ignores: options.common.per_file_ignores.map(|per_file_ignores| { per_file_ignores @@ -1316,6 +1321,7 @@ fn warn_about_deprecated_top_level_lint_options( used_options.push("extend-unsafe-fixes"); } + #[allow(deprecated)] if top_level_options.ignore_init_module_imports.is_some() { used_options.push("ignore-init-module-imports"); } diff --git a/crates/ruff_workspace/src/options.rs b/crates/ruff_workspace/src/options.rs index ad536a3f356319..93cd4bef08391b 100644 --- a/crates/ruff_workspace/src/options.rs +++ b/crates/ruff_workspace/src/options.rs @@ -692,13 +692,18 @@ pub struct LintCommonOptions { /// imports will still be flagged, but with a dedicated message suggesting /// that the import is either added to the module's `__all__` symbol, or /// re-exported with a redundant alias (e.g., `import os as os`). + /// + /// This option is enabled by default. #[option( - default = "false", + default = "true", value_type = "bool", example = r#" - ignore-init-module-imports = true + ignore-init-module-imports = false "# )] + #[deprecated( + note = "`ignore-init-module-imports` is deprecated and is now enabled by default." + )] pub ignore_init_module_imports: Option, /// A list of objects that should be treated equivalently to a diff --git a/ruff.schema.json b/ruff.schema.json index 5412af6e672fda..e73cab8fcd0f6e 100644 --- a/ruff.schema.json +++ b/ruff.schema.json @@ -424,7 +424,7 @@ } }, "ignore-init-module-imports": { - "description": "Avoid automatically removing unused imports in `__init__.py` files. Such imports will still be flagged, but with a dedicated message suggesting that the import is either added to the module's `__all__` symbol, or re-exported with a redundant alias (e.g., `import os as os`).", + "description": "Avoid automatically removing unused imports in `__init__.py` files. Such imports will still be flagged, but with a dedicated message suggesting that the import is either added to the module's `__all__` symbol, or re-exported with a redundant alias (e.g., `import os as os`).\n\nThis option is enabled by default.", "deprecated": true, "type": [ "boolean", @@ -2076,7 +2076,8 @@ } }, "ignore-init-module-imports": { - "description": "Avoid automatically removing unused imports in `__init__.py` files. Such imports will still be flagged, but with a dedicated message suggesting that the import is either added to the module's `__all__` symbol, or re-exported with a redundant alias (e.g., `import os as os`).", + "description": "Avoid automatically removing unused imports in `__init__.py` files. Such imports will still be flagged, but with a dedicated message suggesting that the import is either added to the module's `__all__` symbol, or re-exported with a redundant alias (e.g., `import os as os`).\n\nThis option is enabled by default.", + "deprecated": true, "type": [ "boolean", "null"