From 0e44707bf3d46ae1d8c2452faed539e6e721513c Mon Sep 17 00:00:00 2001 From: Zanie Date: Wed, 30 Aug 2023 10:46:38 -0500 Subject: [PATCH 1/7] Add `--preview` / `--no-preview` to `ruff check` --- crates/ruff/src/settings/defaults.rs | 1 + crates/ruff/src/settings/mod.rs | 1 + crates/ruff_cli/src/args.rs | 10 ++++++++++ crates/ruff_wasm/src/lib.rs | 1 + crates/ruff_workspace/src/configuration.rs | 4 ++++ crates/ruff_workspace/src/options.rs | 11 +++++++++++ 6 files changed, 28 insertions(+) diff --git a/crates/ruff/src/settings/defaults.rs b/crates/ruff/src/settings/defaults.rs index a7ef3d3b88e67..21fe89fa1f9e6 100644 --- a/crates/ruff/src/settings/defaults.rs +++ b/crates/ruff/src/settings/defaults.rs @@ -84,6 +84,7 @@ impl Default for Settings { line_length: LineLength::default(), logger_objects: vec![], namespace_packages: vec![], + preview_mode: false, per_file_ignores: vec![], project_root: path_dedot::CWD.clone(), respect_gitignore: true, diff --git a/crates/ruff/src/settings/mod.rs b/crates/ruff/src/settings/mod.rs index edf5aaaebd8d2..1e15d723fdc04 100644 --- a/crates/ruff/src/settings/mod.rs +++ b/crates/ruff/src/settings/mod.rs @@ -55,6 +55,7 @@ pub struct Settings { pub per_file_ignores: Vec<(GlobMatcher, GlobMatcher, RuleSet)>, pub target_version: PythonVersion, + pub preview_mode: bool, // Resolver settings pub exclude: FilePatternSet, diff --git a/crates/ruff_cli/src/args.rs b/crates/ruff_cli/src/args.rs index 6971867dfc27e..05b40a0170a97 100644 --- a/crates/ruff_cli/src/args.rs +++ b/crates/ruff_cli/src/args.rs @@ -115,6 +115,11 @@ pub struct CheckCommand { /// The minimum Python version that should be supported. #[arg(long, value_enum)] pub target_version: Option, + /// Enable preview mode + #[arg(long, overrides_with("no_preview"))] + preview: bool, + #[clap(long, overrides_with("preview"), hide = true)] + no_preview: bool, /// Path to the `pyproject.toml` or `ruff.toml` file to use for /// configuration. #[arg(long, conflicts_with = "isolated")] @@ -458,6 +463,7 @@ impl CheckCommand { ignore: self.ignore, line_length: self.line_length, per_file_ignores: self.per_file_ignores, + preview_mode: resolve_bool_arg(self.preview, self.no_preview), respect_gitignore: resolve_bool_arg( self.respect_gitignore, self.no_respect_gitignore, @@ -569,6 +575,7 @@ pub struct Overrides { pub ignore: Option>, pub line_length: Option, pub per_file_ignores: Option>, + pub preview_mode: Option, pub respect_gitignore: Option, pub select: Option>, pub show_source: Option, @@ -632,6 +639,9 @@ impl ConfigProcessor for Overrides { if let Some(line_length) = &self.line_length { config.line_length = Some(*line_length); } + if let Some(preview_mode) = &self.preview_mode { + config.preview_mode = Some(*preview_mode); + } if let Some(per_file_ignores) = &self.per_file_ignores { config.per_file_ignores = Some(collect_per_file_ignores(per_file_ignores.clone())); } diff --git a/crates/ruff_wasm/src/lib.rs b/crates/ruff_wasm/src/lib.rs index 07a5b749bb02a..bdb0b3aa450ac 100644 --- a/crates/ruff_wasm/src/lib.rs +++ b/crates/ruff_wasm/src/lib.rs @@ -128,6 +128,7 @@ impl Workspace { external: Some(Vec::default()), ignore: Some(Vec::default()), line_length: Some(LineLength::default()), + preview_mode: Some(false), select: Some(defaults::PREFIXES.to_vec()), tab_size: Some(TabSize::default()), target_version: Some(PythonVersion::default()), diff --git a/crates/ruff_workspace/src/configuration.rs b/crates/ruff_workspace/src/configuration.rs index a7ad93e686ac5..51c2471520085 100644 --- a/crates/ruff_workspace/src/configuration.rs +++ b/crates/ruff_workspace/src/configuration.rs @@ -67,6 +67,7 @@ pub struct Configuration { pub line_length: Option, pub logger_objects: Option>, pub namespace_packages: Option>, + pub preview_mode: Option, pub required_version: Option, pub respect_gitignore: Option, pub show_fixes: Option, @@ -174,6 +175,7 @@ impl Configuration { .collect() }), logger_objects: self.logger_objects.unwrap_or_default(), + preview_mode: self.preview_mode.unwrap_or_default(), typing_modules: self.typing_modules.unwrap_or_default(), // Plugins flake8_annotations: self @@ -387,6 +389,7 @@ impl Configuration { .namespace_packages .map(|namespace_package| resolve_src(&namespace_package, project_root)) .transpose()?, + preview_mode: options.preview_mode, per_file_ignores: options.per_file_ignores.map(|per_file_ignores| { per_file_ignores .into_iter() @@ -676,6 +679,7 @@ impl Configuration { show_fixes: self.show_fixes.or(config.show_fixes), src: self.src.or(config.src), target_version: self.target_version.or(config.target_version), + preview_mode: self.preview_mode.or(config.preview_mode), task_tags: self.task_tags.or(config.task_tags), typing_modules: self.typing_modules.or(config.typing_modules), // Plugins diff --git a/crates/ruff_workspace/src/options.rs b/crates/ruff_workspace/src/options.rs index 599f4626393d3..be48928f7c355 100644 --- a/crates/ruff_workspace/src/options.rs +++ b/crates/ruff_workspace/src/options.rs @@ -482,6 +482,17 @@ pub struct Options { /// field (e.g., `requires-python = ">=3.8"`). If Ruff is configured via /// `ruff.toml` or `.ruff.toml`, no such inference will be performed. pub target_version: Option, + #[option( + default = "false", + value_type = "bool", + example = r#" + # Enable preview mode + preview-mode = true + "# + )] + /// Whether to enable preview mode. When preview mode is enabled, Ruff will + /// use unstable rules and fixes. + pub preview_mode: Option, #[option( default = r#"["TODO", "FIXME", "XXX"]"#, value_type = "list[str]", From a16e3f5a325c5ea41f2cabfadb2e3193d9405632 Mon Sep 17 00:00:00 2001 From: Zanie Date: Wed, 30 Aug 2023 10:49:01 -0500 Subject: [PATCH 2/7] Hide `--preview` for now --- crates/ruff_cli/src/args.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/ruff_cli/src/args.rs b/crates/ruff_cli/src/args.rs index 05b40a0170a97..be2f76c537c05 100644 --- a/crates/ruff_cli/src/args.rs +++ b/crates/ruff_cli/src/args.rs @@ -116,7 +116,7 @@ pub struct CheckCommand { #[arg(long, value_enum)] pub target_version: Option, /// Enable preview mode - #[arg(long, overrides_with("no_preview"))] + #[arg(long, overrides_with("no_preview"), hide = true)] preview: bool, #[clap(long, overrides_with("preview"), hide = true)] no_preview: bool, From fecfcaca2dd9a57181b3173a7f8d1f222679170e Mon Sep 17 00:00:00 2001 From: Zanie Date: Wed, 30 Aug 2023 10:53:53 -0500 Subject: [PATCH 3/7] Update schema --- ruff.schema.json | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/ruff.schema.json b/ruff.schema.json index 0e09a47eb91cc..8d94de45dd0a4 100644 --- a/ruff.schema.json +++ b/ruff.schema.json @@ -441,6 +441,13 @@ } } }, + "preview-mode": { + "description": "Whether to enable preview mode. When preview mode is enabled, Ruff will use unstable rules and fixes.", + "type": [ + "boolean", + "null" + ] + }, "pycodestyle": { "description": "Options for the `pycodestyle` plugin.", "anyOf": [ From 475769da3383d9983d49d68afb468b34a1287f99 Mon Sep 17 00:00:00 2001 From: Zanie Date: Wed, 30 Aug 2023 11:03:00 -0500 Subject: [PATCH 4/7] Rename to `preview`; extend CLI help string --- crates/ruff/src/settings/defaults.rs | 2 +- crates/ruff/src/settings/mod.rs | 2 +- crates/ruff_cli/src/args.rs | 10 +++++----- crates/ruff_wasm/src/lib.rs | 2 +- crates/ruff_workspace/src/configuration.rs | 8 ++++---- crates/ruff_workspace/src/options.rs | 6 +++--- ruff.schema.json | 2 +- 7 files changed, 16 insertions(+), 16 deletions(-) diff --git a/crates/ruff/src/settings/defaults.rs b/crates/ruff/src/settings/defaults.rs index 21fe89fa1f9e6..efbfdf29455e0 100644 --- a/crates/ruff/src/settings/defaults.rs +++ b/crates/ruff/src/settings/defaults.rs @@ -84,7 +84,7 @@ impl Default for Settings { line_length: LineLength::default(), logger_objects: vec![], namespace_packages: vec![], - preview_mode: false, + preview: false, per_file_ignores: vec![], project_root: path_dedot::CWD.clone(), respect_gitignore: true, diff --git a/crates/ruff/src/settings/mod.rs b/crates/ruff/src/settings/mod.rs index 1e15d723fdc04..27f72587cc255 100644 --- a/crates/ruff/src/settings/mod.rs +++ b/crates/ruff/src/settings/mod.rs @@ -55,7 +55,7 @@ pub struct Settings { pub per_file_ignores: Vec<(GlobMatcher, GlobMatcher, RuleSet)>, pub target_version: PythonVersion, - pub preview_mode: bool, + pub preview: bool, // Resolver settings pub exclude: FilePatternSet, diff --git a/crates/ruff_cli/src/args.rs b/crates/ruff_cli/src/args.rs index be2f76c537c05..2e6469b876adf 100644 --- a/crates/ruff_cli/src/args.rs +++ b/crates/ruff_cli/src/args.rs @@ -115,7 +115,7 @@ pub struct CheckCommand { /// The minimum Python version that should be supported. #[arg(long, value_enum)] pub target_version: Option, - /// Enable preview mode + /// Enable preview mode; checks will include unstable rules and fixes. #[arg(long, overrides_with("no_preview"), hide = true)] preview: bool, #[clap(long, overrides_with("preview"), hide = true)] @@ -463,7 +463,7 @@ impl CheckCommand { ignore: self.ignore, line_length: self.line_length, per_file_ignores: self.per_file_ignores, - preview_mode: resolve_bool_arg(self.preview, self.no_preview), + preview: resolve_bool_arg(self.preview, self.no_preview), respect_gitignore: resolve_bool_arg( self.respect_gitignore, self.no_respect_gitignore, @@ -575,7 +575,7 @@ pub struct Overrides { pub ignore: Option>, pub line_length: Option, pub per_file_ignores: Option>, - pub preview_mode: Option, + pub preview: Option, pub respect_gitignore: Option, pub select: Option>, pub show_source: Option, @@ -639,8 +639,8 @@ impl ConfigProcessor for Overrides { if let Some(line_length) = &self.line_length { config.line_length = Some(*line_length); } - if let Some(preview_mode) = &self.preview_mode { - config.preview_mode = Some(*preview_mode); + if let Some(preview) = &self.preview { + config.preview = Some(*preview); } if let Some(per_file_ignores) = &self.per_file_ignores { config.per_file_ignores = Some(collect_per_file_ignores(per_file_ignores.clone())); diff --git a/crates/ruff_wasm/src/lib.rs b/crates/ruff_wasm/src/lib.rs index bdb0b3aa450ac..799963dc4da04 100644 --- a/crates/ruff_wasm/src/lib.rs +++ b/crates/ruff_wasm/src/lib.rs @@ -128,7 +128,7 @@ impl Workspace { external: Some(Vec::default()), ignore: Some(Vec::default()), line_length: Some(LineLength::default()), - preview_mode: Some(false), + preview: Some(false), select: Some(defaults::PREFIXES.to_vec()), tab_size: Some(TabSize::default()), target_version: Some(PythonVersion::default()), diff --git a/crates/ruff_workspace/src/configuration.rs b/crates/ruff_workspace/src/configuration.rs index 51c2471520085..89a5d9d6f1ad8 100644 --- a/crates/ruff_workspace/src/configuration.rs +++ b/crates/ruff_workspace/src/configuration.rs @@ -67,7 +67,7 @@ pub struct Configuration { pub line_length: Option, pub logger_objects: Option>, pub namespace_packages: Option>, - pub preview_mode: Option, + pub preview: Option, pub required_version: Option, pub respect_gitignore: Option, pub show_fixes: Option, @@ -175,7 +175,7 @@ impl Configuration { .collect() }), logger_objects: self.logger_objects.unwrap_or_default(), - preview_mode: self.preview_mode.unwrap_or_default(), + preview: self.preview.unwrap_or_default(), typing_modules: self.typing_modules.unwrap_or_default(), // Plugins flake8_annotations: self @@ -389,7 +389,7 @@ impl Configuration { .namespace_packages .map(|namespace_package| resolve_src(&namespace_package, project_root)) .transpose()?, - preview_mode: options.preview_mode, + preview: options.preview, per_file_ignores: options.per_file_ignores.map(|per_file_ignores| { per_file_ignores .into_iter() @@ -679,7 +679,7 @@ impl Configuration { show_fixes: self.show_fixes.or(config.show_fixes), src: self.src.or(config.src), target_version: self.target_version.or(config.target_version), - preview_mode: self.preview_mode.or(config.preview_mode), + preview: self.preview.or(config.preview), task_tags: self.task_tags.or(config.task_tags), typing_modules: self.typing_modules.or(config.typing_modules), // Plugins diff --git a/crates/ruff_workspace/src/options.rs b/crates/ruff_workspace/src/options.rs index be48928f7c355..dc711227986a9 100644 --- a/crates/ruff_workspace/src/options.rs +++ b/crates/ruff_workspace/src/options.rs @@ -486,13 +486,13 @@ pub struct Options { default = "false", value_type = "bool", example = r#" - # Enable preview mode - preview-mode = true + # Enable preview features + preview = true "# )] /// Whether to enable preview mode. When preview mode is enabled, Ruff will /// use unstable rules and fixes. - pub preview_mode: Option, + pub preview: Option, #[option( default = r#"["TODO", "FIXME", "XXX"]"#, value_type = "list[str]", diff --git a/ruff.schema.json b/ruff.schema.json index 8d94de45dd0a4..bce237bcb1a6d 100644 --- a/ruff.schema.json +++ b/ruff.schema.json @@ -441,7 +441,7 @@ } } }, - "preview-mode": { + "preview": { "description": "Whether to enable preview mode. When preview mode is enabled, Ruff will use unstable rules and fixes.", "type": [ "boolean", From 29be01af914c3fb414eca21f60cd76c54c2441b7 Mon Sep 17 00:00:00 2001 From: Zanie Date: Wed, 30 Aug 2023 11:22:27 -0500 Subject: [PATCH 5/7] Add `PreviewMode` enum --- crates/ruff/src/settings/defaults.rs | 4 ++-- crates/ruff/src/settings/mod.rs | 3 ++- crates/ruff/src/settings/types.rs | 16 ++++++++++++++++ crates/ruff_cli/src/args.rs | 6 +++--- crates/ruff_wasm/src/lib.rs | 2 +- crates/ruff_workspace/src/configuration.rs | 7 ++++--- 6 files changed, 28 insertions(+), 10 deletions(-) diff --git a/crates/ruff/src/settings/defaults.rs b/crates/ruff/src/settings/defaults.rs index efbfdf29455e0..69dd446e3e254 100644 --- a/crates/ruff/src/settings/defaults.rs +++ b/crates/ruff/src/settings/defaults.rs @@ -4,7 +4,7 @@ use regex::Regex; use rustc_hash::FxHashSet; use std::collections::HashSet; -use super::types::{FilePattern, PythonVersion}; +use super::types::{FilePattern, PreviewMode, PythonVersion}; use super::Settings; use crate::codes::{self, RuleCodePrefix}; use crate::line_width::{LineLength, TabSize}; @@ -84,7 +84,7 @@ impl Default for Settings { line_length: LineLength::default(), logger_objects: vec![], namespace_packages: vec![], - preview: false, + preview: PreviewMode::default(), per_file_ignores: vec![], project_root: path_dedot::CWD.clone(), respect_gitignore: true, diff --git a/crates/ruff/src/settings/mod.rs b/crates/ruff/src/settings/mod.rs index 27f72587cc255..ea5d38aa98830 100644 --- a/crates/ruff/src/settings/mod.rs +++ b/crates/ruff/src/settings/mod.rs @@ -24,6 +24,7 @@ use crate::settings::types::{FilePatternSet, PerFileIgnore, PythonVersion, Seria use super::line_width::{LineLength, TabSize}; use self::rule_table::RuleTable; +use self::types::PreviewMode; pub mod defaults; pub mod flags; @@ -55,7 +56,7 @@ pub struct Settings { pub per_file_ignores: Vec<(GlobMatcher, GlobMatcher, RuleSet)>, pub target_version: PythonVersion, - pub preview: bool, + pub preview: PreviewMode, // Resolver settings pub exclude: FilePatternSet, diff --git a/crates/ruff/src/settings/types.rs b/crates/ruff/src/settings/types.rs index 8461604425ba4..60944d2b83f39 100644 --- a/crates/ruff/src/settings/types.rs +++ b/crates/ruff/src/settings/types.rs @@ -92,6 +92,22 @@ impl PythonVersion { } } +#[derive(Clone, Copy, Debug, PartialOrd, Ord, PartialEq, Eq, Default, CacheKey, is_macro::Is)] +pub enum PreviewMode { + Enabled, + #[default] + Disabled, +} + +impl From for PreviewMode { + fn from(version: bool) -> Self { + match version { + true => PreviewMode::Enabled, + false => PreviewMode::Disabled, + } + } +} + #[derive(Debug, Clone, CacheKey, PartialEq, PartialOrd, Eq, Ord)] pub enum FilePattern { Builtin(&'static str), diff --git a/crates/ruff_cli/src/args.rs b/crates/ruff_cli/src/args.rs index 2e6469b876adf..50cb562c1d4b2 100644 --- a/crates/ruff_cli/src/args.rs +++ b/crates/ruff_cli/src/args.rs @@ -9,7 +9,7 @@ use rustc_hash::FxHashMap; use ruff::logging::LogLevel; use ruff::registry::Rule; use ruff::settings::types::{ - FilePattern, PatternPrefixPair, PerFileIgnore, PythonVersion, SerializationFormat, + FilePattern, PatternPrefixPair, PerFileIgnore, PreviewMode, PythonVersion, SerializationFormat, }; use ruff::RuleSelector; use ruff_workspace::configuration::{Configuration, RuleSelection}; @@ -463,7 +463,7 @@ impl CheckCommand { ignore: self.ignore, line_length: self.line_length, per_file_ignores: self.per_file_ignores, - preview: resolve_bool_arg(self.preview, self.no_preview), + preview: resolve_bool_arg(self.preview, self.no_preview).map(PreviewMode::from), respect_gitignore: resolve_bool_arg( self.respect_gitignore, self.no_respect_gitignore, @@ -575,7 +575,7 @@ pub struct Overrides { pub ignore: Option>, pub line_length: Option, pub per_file_ignores: Option>, - pub preview: Option, + pub preview: Option, pub respect_gitignore: Option, pub select: Option>, pub show_source: Option, diff --git a/crates/ruff_wasm/src/lib.rs b/crates/ruff_wasm/src/lib.rs index 799963dc4da04..b903ac8f1c67e 100644 --- a/crates/ruff_wasm/src/lib.rs +++ b/crates/ruff_wasm/src/lib.rs @@ -8,7 +8,7 @@ use ruff::directives; use ruff::line_width::{LineLength, TabSize}; use ruff::linter::{check_path, LinterResult}; use ruff::registry::AsRule; -use ruff::settings::types::PythonVersion; +use ruff::settings::types::{PythonVersion}; use ruff::settings::{defaults, flags, Settings}; use ruff_formatter::{FormatResult, Formatted}; use ruff_python_ast::{Mod, PySourceType}; diff --git a/crates/ruff_workspace/src/configuration.rs b/crates/ruff_workspace/src/configuration.rs index 89a5d9d6f1ad8..68a505adb0258 100644 --- a/crates/ruff_workspace/src/configuration.rs +++ b/crates/ruff_workspace/src/configuration.rs @@ -23,7 +23,8 @@ use ruff::registry::{Rule, RuleSet, INCOMPATIBLE_CODES}; use ruff::rule_selector::Specificity; use ruff::settings::rule_table::RuleTable; use ruff::settings::types::{ - FilePattern, FilePatternSet, PerFileIgnore, PythonVersion, SerializationFormat, Version, + FilePattern, FilePatternSet, PerFileIgnore, PreviewMode, PythonVersion, SerializationFormat, + Version, }; use ruff::settings::{defaults, resolve_per_file_ignores, AllSettings, CliSettings, Settings}; use ruff::{fs, warn_user_once_by_id, RuleSelector, RUFF_PKG_VERSION}; @@ -67,7 +68,7 @@ pub struct Configuration { pub line_length: Option, pub logger_objects: Option>, pub namespace_packages: Option>, - pub preview: Option, + pub preview: Option, pub required_version: Option, pub respect_gitignore: Option, pub show_fixes: Option, @@ -389,7 +390,7 @@ impl Configuration { .namespace_packages .map(|namespace_package| resolve_src(&namespace_package, project_root)) .transpose()?, - preview: options.preview, + preview: options.preview.map(PreviewMode::from), per_file_ignores: options.per_file_ignores.map(|per_file_ignores| { per_file_ignores .into_iter() From 432f09bcffb73c2ca13a4f3dd9ec3cc0acf41779 Mon Sep 17 00:00:00 2001 From: Zanie Date: Wed, 30 Aug 2023 11:25:15 -0500 Subject: [PATCH 6/7] Lint --- crates/ruff/src/settings/types.rs | 7 ++++--- crates/ruff_wasm/src/lib.rs | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/crates/ruff/src/settings/types.rs b/crates/ruff/src/settings/types.rs index 60944d2b83f39..4fb66ccf7fb8e 100644 --- a/crates/ruff/src/settings/types.rs +++ b/crates/ruff/src/settings/types.rs @@ -101,9 +101,10 @@ pub enum PreviewMode { impl From for PreviewMode { fn from(version: bool) -> Self { - match version { - true => PreviewMode::Enabled, - false => PreviewMode::Disabled, + if version { + PreviewMode::Enabled + } else { + PreviewMode::Disabled } } } diff --git a/crates/ruff_wasm/src/lib.rs b/crates/ruff_wasm/src/lib.rs index b903ac8f1c67e..799963dc4da04 100644 --- a/crates/ruff_wasm/src/lib.rs +++ b/crates/ruff_wasm/src/lib.rs @@ -8,7 +8,7 @@ use ruff::directives; use ruff::line_width::{LineLength, TabSize}; use ruff::linter::{check_path, LinterResult}; use ruff::registry::AsRule; -use ruff::settings::types::{PythonVersion}; +use ruff::settings::types::PythonVersion; use ruff::settings::{defaults, flags, Settings}; use ruff_formatter::{FormatResult, Formatted}; use ruff_python_ast::{Mod, PySourceType}; From 05810f221df903fa726a5217a53cbea6072df5a2 Mon Sep 17 00:00:00 2001 From: Zanie Date: Thu, 31 Aug 2023 08:50:38 -0500 Subject: [PATCH 7/7] Touchup to `PreviewMode` --- crates/ruff/src/settings/types.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/ruff/src/settings/types.rs b/crates/ruff/src/settings/types.rs index 4fb66ccf7fb8e..bb293d3986bca 100644 --- a/crates/ruff/src/settings/types.rs +++ b/crates/ruff/src/settings/types.rs @@ -92,11 +92,11 @@ impl PythonVersion { } } -#[derive(Clone, Copy, Debug, PartialOrd, Ord, PartialEq, Eq, Default, CacheKey, is_macro::Is)] +#[derive(Clone, Copy, Debug, PartialEq, Eq, Default, CacheKey, is_macro::Is)] pub enum PreviewMode { - Enabled, #[default] Disabled, + Enabled, } impl From for PreviewMode {