From e6be85fb8bb04b6c4a941464f66c9b47e01b3541 Mon Sep 17 00:00:00 2001 From: Ben Doerry Date: Sun, 14 May 2023 14:50:56 +0100 Subject: [PATCH 1/5] Introduce `CombinePluginOptions` trait This is to enable overriding sub settings without nulling out other inherited settings. --- crates/ruff/src/settings/configuration.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/crates/ruff/src/settings/configuration.rs b/crates/ruff/src/settings/configuration.rs index 5e6318e5cb0a5..d212277287ac2 100644 --- a/crates/ruff/src/settings/configuration.rs +++ b/crates/ruff/src/settings/configuration.rs @@ -299,6 +299,22 @@ impl Configuration { } } +pub trait CombinePluginOptions { + #[must_use] + fn combine(self, other: Self) -> Self; +} + +impl CombinePluginOptions for Option { + fn combine(self, other: Self) -> Self { + match (self, other) { + (Some(base), Some(other)) => Some(base.combine(other)), + (Some(base), None) => Some(base), + (None, Some(other)) => Some(other), + (None, None) => None, + } + } +} + /// Given a list of source paths, which could include glob patterns, resolve the /// matching paths. pub fn resolve_src(src: &[String], project_root: &Path) -> Result> { From e1bfdeadd324318866714967aedcc0683868e5c2 Mon Sep 17 00:00:00 2001 From: Ben Doerry Date: Sun, 14 May 2023 14:55:58 +0100 Subject: [PATCH 2/5] Implement `CombinePluginOptions` for all plugins To be honest this isn't quite "all" plugins, just those we already combine in `Configuration`. Fixes #4348 --- .../src/rules/flake8_annotations/settings.rs | 16 +++++++ .../ruff/src/rules/flake8_bandit/settings.rs | 16 +++++++ .../ruff/src/rules/flake8_bugbear/settings.rs | 10 ++++ .../src/rules/flake8_builtins/settings.rs | 10 ++++ .../rules/flake8_comprehensions/settings.rs | 12 +++++ .../ruff/src/rules/flake8_errmsg/settings.rs | 10 ++++ .../ruff/src/rules/flake8_gettext/settings.rs | 15 ++++++ .../flake8_implicit_str_concat/settings.rs | 10 ++++ .../flake8_import_conventions/settings.rs | 13 ++++++ .../src/rules/flake8_pytest_style/settings.rs | 24 ++++++++++ .../ruff/src/rules/flake8_quotes/settings.rs | 13 ++++++ crates/ruff/src/rules/flake8_self/settings.rs | 9 ++++ .../src/rules/flake8_tidy_imports/options.rs | 11 +++++ .../rules/flake8_type_checking/settings.rs | 17 +++++++ .../rules/flake8_unused_arguments/settings.rs | 9 ++++ crates/ruff/src/rules/isort/settings.rs | 35 ++++++++++++++ crates/ruff/src/rules/mccabe/settings.rs | 10 ++++ crates/ruff/src/rules/pep8_naming/settings.rs | 14 ++++++ crates/ruff/src/rules/pycodestyle/settings.rs | 13 ++++++ crates/ruff/src/rules/pydocstyle/settings.rs | 12 ++++- crates/ruff/src/rules/pylint/settings.rs | 16 +++++++ crates/ruff/src/settings/configuration.rs | 46 ++++++++++--------- 22 files changed, 319 insertions(+), 22 deletions(-) diff --git a/crates/ruff/src/rules/flake8_annotations/settings.rs b/crates/ruff/src/rules/flake8_annotations/settings.rs index 8b7e3c0f6e029..6eaa39257920a 100644 --- a/crates/ruff/src/rules/flake8_annotations/settings.rs +++ b/crates/ruff/src/rules/flake8_annotations/settings.rs @@ -5,6 +5,8 @@ use serde::{Deserialize, Serialize}; use ruff_macros::CacheKey; use ruff_macros::ConfigurationOptions; +use crate::settings::configuration::CombinePluginOptions; + #[derive(Debug, PartialEq, Eq, Default, Serialize, Deserialize, ConfigurationOptions)] #[serde( deny_unknown_fields, @@ -93,3 +95,17 @@ impl From for Options { } } } + +impl CombinePluginOptions for Options { + fn combine(self, other: Self) -> Self { + Self { + mypy_init_return: self.mypy_init_return.or(other.mypy_init_return), + suppress_dummy_args: self.suppress_dummy_args.or(other.suppress_dummy_args), + suppress_none_returning: self + .suppress_none_returning + .or(other.suppress_none_returning), + allow_star_arg_any: self.allow_star_arg_any.or(other.allow_star_arg_any), + ignore_fully_untyped: self.ignore_fully_untyped.or(other.ignore_fully_untyped), + } + } +} diff --git a/crates/ruff/src/rules/flake8_bandit/settings.rs b/crates/ruff/src/rules/flake8_bandit/settings.rs index 0334721a31872..595c2d866c7e0 100644 --- a/crates/ruff/src/rules/flake8_bandit/settings.rs +++ b/crates/ruff/src/rules/flake8_bandit/settings.rs @@ -4,6 +4,8 @@ use serde::{Deserialize, Serialize}; use ruff_macros::{CacheKey, ConfigurationOptions}; +use crate::settings::configuration::CombinePluginOptions; + fn default_tmp_dirs() -> Vec { ["/tmp", "/var/tmp", "/dev/shm"] .map(std::string::ToString::to_string) @@ -87,3 +89,17 @@ impl Default for Settings { } } } + +impl CombinePluginOptions for Options { + fn combine(self, other: Self) -> Self { + Self { + hardcoded_tmp_directory: self + .hardcoded_tmp_directory + .or(other.hardcoded_tmp_directory), + hardcoded_tmp_directory_extend: self + .hardcoded_tmp_directory_extend + .or(other.hardcoded_tmp_directory_extend), + check_typed_exception: self.check_typed_exception.or(other.check_typed_exception), + } + } +} diff --git a/crates/ruff/src/rules/flake8_bugbear/settings.rs b/crates/ruff/src/rules/flake8_bugbear/settings.rs index 95e00f92d3481..d4e8eb778e1c2 100644 --- a/crates/ruff/src/rules/flake8_bugbear/settings.rs +++ b/crates/ruff/src/rules/flake8_bugbear/settings.rs @@ -4,6 +4,8 @@ use serde::{Deserialize, Serialize}; use ruff_macros::{CacheKey, ConfigurationOptions}; +use crate::settings::configuration::CombinePluginOptions; + #[derive(Debug, PartialEq, Eq, Default, Serialize, Deserialize, ConfigurationOptions)] #[serde( deny_unknown_fields, @@ -46,3 +48,11 @@ impl From for Options { } } } + +impl CombinePluginOptions for Options { + fn combine(self, other: Self) -> Self { + Self { + extend_immutable_calls: self.extend_immutable_calls.or(other.extend_immutable_calls), + } + } +} diff --git a/crates/ruff/src/rules/flake8_builtins/settings.rs b/crates/ruff/src/rules/flake8_builtins/settings.rs index a9c6228f26623..8546cf41fc4ac 100644 --- a/crates/ruff/src/rules/flake8_builtins/settings.rs +++ b/crates/ruff/src/rules/flake8_builtins/settings.rs @@ -4,6 +4,8 @@ use serde::{Deserialize, Serialize}; use ruff_macros::{CacheKey, ConfigurationOptions}; +use crate::settings::configuration::CombinePluginOptions; + #[derive(Debug, PartialEq, Eq, Default, Serialize, Deserialize, ConfigurationOptions)] #[serde( deny_unknown_fields, @@ -41,3 +43,11 @@ impl From for Options { } } } + +impl CombinePluginOptions for Options { + fn combine(self, other: Self) -> Self { + Self { + builtins_ignorelist: self.builtins_ignorelist.or(other.builtins_ignorelist), + } + } +} diff --git a/crates/ruff/src/rules/flake8_comprehensions/settings.rs b/crates/ruff/src/rules/flake8_comprehensions/settings.rs index 630f473de286b..26cac2c5a743a 100644 --- a/crates/ruff/src/rules/flake8_comprehensions/settings.rs +++ b/crates/ruff/src/rules/flake8_comprehensions/settings.rs @@ -4,6 +4,8 @@ use serde::{Deserialize, Serialize}; use ruff_macros::{CacheKey, ConfigurationOptions}; +use crate::settings::configuration::CombinePluginOptions; + #[derive(Debug, PartialEq, Eq, Default, Serialize, Deserialize, ConfigurationOptions)] #[serde( deny_unknown_fields, @@ -45,3 +47,13 @@ impl From for Options { } } } + +impl CombinePluginOptions for Options { + fn combine(self, other: Self) -> Self { + Self { + allow_dict_calls_with_keyword_arguments: self + .allow_dict_calls_with_keyword_arguments + .or(other.allow_dict_calls_with_keyword_arguments), + } + } +} diff --git a/crates/ruff/src/rules/flake8_errmsg/settings.rs b/crates/ruff/src/rules/flake8_errmsg/settings.rs index efa3bd93458ed..5c5558356e7d3 100644 --- a/crates/ruff/src/rules/flake8_errmsg/settings.rs +++ b/crates/ruff/src/rules/flake8_errmsg/settings.rs @@ -4,6 +4,8 @@ use serde::{Deserialize, Serialize}; use ruff_macros::{CacheKey, ConfigurationOptions}; +use crate::settings::configuration::CombinePluginOptions; + #[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Default, ConfigurationOptions)] #[serde( deny_unknown_fields, @@ -37,3 +39,11 @@ impl From for Options { } } } + +impl CombinePluginOptions for Options { + fn combine(self, other: Self) -> Self { + Self { + max_string_length: self.max_string_length.or(other.max_string_length), + } + } +} diff --git a/crates/ruff/src/rules/flake8_gettext/settings.rs b/crates/ruff/src/rules/flake8_gettext/settings.rs index 6146f6f934f7a..0deef220fffc2 100644 --- a/crates/ruff/src/rules/flake8_gettext/settings.rs +++ b/crates/ruff/src/rules/flake8_gettext/settings.rs @@ -2,6 +2,8 @@ use serde::{Deserialize, Serialize}; use ruff_macros::{CacheKey, ConfigurationOptions}; +use crate::settings::configuration::CombinePluginOptions; + #[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Default, ConfigurationOptions)] #[serde( deny_unknown_fields, @@ -69,3 +71,16 @@ impl From for Options { } } } + +impl CombinePluginOptions for Options { + fn combine(self, other: Self) -> Self { + Self { + function_names: self.function_names.or(other.function_names), + extend_function_names: other + .extend_function_names + .into_iter() + .chain(self.extend_function_names.into_iter()) + .collect(), + } + } +} diff --git a/crates/ruff/src/rules/flake8_implicit_str_concat/settings.rs b/crates/ruff/src/rules/flake8_implicit_str_concat/settings.rs index 3a9a18bb1ec03..3b16f341395d7 100644 --- a/crates/ruff/src/rules/flake8_implicit_str_concat/settings.rs +++ b/crates/ruff/src/rules/flake8_implicit_str_concat/settings.rs @@ -4,6 +4,8 @@ use serde::{Deserialize, Serialize}; use ruff_macros::{CacheKey, ConfigurationOptions}; +use crate::settings::configuration::CombinePluginOptions; + #[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Default, ConfigurationOptions)] #[serde( deny_unknown_fields, @@ -59,3 +61,11 @@ impl From for Options { } } } + +impl CombinePluginOptions for Options { + fn combine(self, other: Self) -> Self { + Self { + allow_multiline: self.allow_multiline.or(other.allow_multiline), + } + } +} diff --git a/crates/ruff/src/rules/flake8_import_conventions/settings.rs b/crates/ruff/src/rules/flake8_import_conventions/settings.rs index a3e45fd654ea4..953d50de10723 100644 --- a/crates/ruff/src/rules/flake8_import_conventions/settings.rs +++ b/crates/ruff/src/rules/flake8_import_conventions/settings.rs @@ -5,6 +5,8 @@ use serde::{Deserialize, Serialize}; use ruff_macros::{CacheKey, ConfigurationOptions}; +use crate::settings::configuration::CombinePluginOptions; + const CONVENTIONAL_ALIASES: &[(&str, &str)] = &[ ("altair", "alt"), ("matplotlib", "mpl"), @@ -132,3 +134,14 @@ impl From for Options { } } } + +impl CombinePluginOptions for Options { + fn combine(self, other: Self) -> Self { + Self { + aliases: self.aliases.or(other.aliases), + extend_aliases: self.extend_aliases.or(other.extend_aliases), + banned_aliases: self.banned_aliases.or(other.banned_aliases), + banned_from: self.banned_from.or(other.banned_from), + } + } +} diff --git a/crates/ruff/src/rules/flake8_pytest_style/settings.rs b/crates/ruff/src/rules/flake8_pytest_style/settings.rs index 887c9fba74228..ce30b99db1868 100644 --- a/crates/ruff/src/rules/flake8_pytest_style/settings.rs +++ b/crates/ruff/src/rules/flake8_pytest_style/settings.rs @@ -4,6 +4,8 @@ use serde::{Deserialize, Serialize}; use ruff_macros::{CacheKey, ConfigurationOptions}; +use crate::settings::configuration::CombinePluginOptions; + use super::types; fn default_broad_exceptions() -> Vec { @@ -167,3 +169,25 @@ impl Default for Settings { } } } + +impl CombinePluginOptions for Options { + fn combine(self, other: Self) -> Self { + Self { + fixture_parentheses: self.fixture_parentheses.or(other.fixture_parentheses), + parametrize_names_type: self.parametrize_names_type.or(other.parametrize_names_type), + parametrize_values_type: self + .parametrize_values_type + .or(other.parametrize_values_type), + parametrize_values_row_type: self + .parametrize_values_row_type + .or(other.parametrize_values_row_type), + raises_require_match_for: self + .raises_require_match_for + .or(other.raises_require_match_for), + raises_extend_require_match_for: self + .raises_extend_require_match_for + .or(other.raises_extend_require_match_for), + mark_parentheses: self.mark_parentheses.or(other.mark_parentheses), + } + } +} diff --git a/crates/ruff/src/rules/flake8_quotes/settings.rs b/crates/ruff/src/rules/flake8_quotes/settings.rs index c64108a00501e..c3781ec99dacb 100644 --- a/crates/ruff/src/rules/flake8_quotes/settings.rs +++ b/crates/ruff/src/rules/flake8_quotes/settings.rs @@ -4,6 +4,8 @@ use serde::{Deserialize, Serialize}; use ruff_macros::{CacheKey, ConfigurationOptions}; +use crate::settings::configuration::CombinePluginOptions; + #[derive(Debug, Copy, Clone, PartialEq, Eq, Serialize, Deserialize, CacheKey)] #[serde(deny_unknown_fields, rename_all = "kebab-case")] #[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] @@ -111,3 +113,14 @@ impl From for Options { } } } + +impl CombinePluginOptions for Options { + fn combine(self, other: Self) -> Self { + Self { + inline_quotes: self.inline_quotes.or(other.inline_quotes), + multiline_quotes: self.multiline_quotes.or(other.multiline_quotes), + docstring_quotes: self.docstring_quotes.or(other.docstring_quotes), + avoid_escape: self.avoid_escape.or(other.avoid_escape), + } + } +} diff --git a/crates/ruff/src/rules/flake8_self/settings.rs b/crates/ruff/src/rules/flake8_self/settings.rs index c03fab8235712..248ddac8a2c2c 100644 --- a/crates/ruff/src/rules/flake8_self/settings.rs +++ b/crates/ruff/src/rules/flake8_self/settings.rs @@ -4,6 +4,8 @@ use serde::{Deserialize, Serialize}; use ruff_macros::{CacheKey, ConfigurationOptions}; +use crate::settings::configuration::CombinePluginOptions; + // By default, ignore the `namedtuple` methods and attributes, which are underscore-prefixed to // prevent conflicts with field names. const IGNORE_NAMES: [&str; 5] = ["_make", "_asdict", "_replace", "_fields", "_field_defaults"]; @@ -57,3 +59,10 @@ impl From for Options { } } } +impl CombinePluginOptions for Options { + fn combine(self, other: Self) -> Self { + Self { + ignore_names: self.ignore_names.or(other.ignore_names), + } + } +} diff --git a/crates/ruff/src/rules/flake8_tidy_imports/options.rs b/crates/ruff/src/rules/flake8_tidy_imports/options.rs index 37dcc87b82331..e99225e81bd2c 100644 --- a/crates/ruff/src/rules/flake8_tidy_imports/options.rs +++ b/crates/ruff/src/rules/flake8_tidy_imports/options.rs @@ -5,6 +5,8 @@ use serde::{Deserialize, Serialize}; use ruff_macros::ConfigurationOptions; +use crate::settings::configuration::CombinePluginOptions; + use super::banned_api::ApiBan; use super::relative_imports::Strictness; use super::Settings; @@ -60,3 +62,12 @@ impl From for Options { } } } + +impl CombinePluginOptions for Options { + fn combine(self, other: Self) -> Self { + Self { + ban_relative_imports: self.ban_relative_imports.or(other.ban_relative_imports), + banned_api: self.banned_api.or(other.banned_api), + } + } +} diff --git a/crates/ruff/src/rules/flake8_type_checking/settings.rs b/crates/ruff/src/rules/flake8_type_checking/settings.rs index a400334b31f1e..e5112ba110faf 100644 --- a/crates/ruff/src/rules/flake8_type_checking/settings.rs +++ b/crates/ruff/src/rules/flake8_type_checking/settings.rs @@ -4,6 +4,8 @@ use serde::{Deserialize, Serialize}; use ruff_macros::{CacheKey, ConfigurationOptions}; +use crate::settings::configuration::CombinePluginOptions; + #[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Default, ConfigurationOptions)] #[serde( deny_unknown_fields, @@ -99,3 +101,18 @@ impl From for Options { } } } + +impl CombinePluginOptions for Options { + fn combine(self, other: Self) -> Self { + Self { + strict: self.strict.or(other.strict), + exempt_modules: self.exempt_modules.or(other.exempt_modules), + runtime_evaluated_base_classes: self + .runtime_evaluated_base_classes + .or(other.runtime_evaluated_base_classes), + runtime_evaluated_decorators: self + .runtime_evaluated_decorators + .or(other.runtime_evaluated_decorators), + } + } +} diff --git a/crates/ruff/src/rules/flake8_unused_arguments/settings.rs b/crates/ruff/src/rules/flake8_unused_arguments/settings.rs index 8500616aeb4d3..8b4f8e47ed388 100644 --- a/crates/ruff/src/rules/flake8_unused_arguments/settings.rs +++ b/crates/ruff/src/rules/flake8_unused_arguments/settings.rs @@ -4,6 +4,8 @@ use serde::{Deserialize, Serialize}; use ruff_macros::{CacheKey, ConfigurationOptions}; +use crate::settings::configuration::CombinePluginOptions; + #[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Default, ConfigurationOptions)] #[serde( deny_unknown_fields, @@ -41,3 +43,10 @@ impl From for Options { } } } +impl CombinePluginOptions for Options { + fn combine(self, other: Self) -> Self { + Self { + ignore_variadic_names: self.ignore_variadic_names.or(other.ignore_variadic_names), + } + } +} diff --git a/crates/ruff/src/rules/isort/settings.rs b/crates/ruff/src/rules/isort/settings.rs index 35a3a8a417cda..8bbf1d16682cb 100644 --- a/crates/ruff/src/rules/isort/settings.rs +++ b/crates/ruff/src/rules/isort/settings.rs @@ -11,6 +11,7 @@ use ruff_macros::{CacheKey, ConfigurationOptions}; use crate::rules::isort::categorize::KnownModules; use crate::rules::isort::ImportType; +use crate::settings::configuration::CombinePluginOptions; use crate::warn_user_once; use super::categorize::ImportSection; @@ -506,3 +507,37 @@ impl From for Options { } } } + +impl CombinePluginOptions for Options { + fn combine(self, other: Self) -> Self { + Self { + force_wrap_aliases: self.force_wrap_aliases.or(other.force_wrap_aliases), + force_single_line: self.force_single_line.or(other.force_single_line), + single_line_exclusions: self.single_line_exclusions.or(other.single_line_exclusions), + combine_as_imports: self.combine_as_imports.or(other.combine_as_imports), + split_on_trailing_comma: self + .split_on_trailing_comma + .or(other.split_on_trailing_comma), + order_by_type: self.order_by_type.or(other.order_by_type), + force_sort_within_sections: self + .force_sort_within_sections + .or(other.force_sort_within_sections), + force_to_top: self.force_to_top.or(other.force_to_top), + known_first_party: self.known_first_party.or(other.known_first_party), + known_third_party: self.known_third_party.or(other.known_third_party), + known_local_folder: self.known_local_folder.or(other.known_local_folder), + extra_standard_library: self.extra_standard_library.or(other.extra_standard_library), + relative_imports_order: self.relative_imports_order.or(other.relative_imports_order), + required_imports: self.required_imports.or(other.required_imports), + classes: self.classes.or(other.classes), + constants: self.constants.or(other.constants), + variables: self.variables.or(other.variables), + no_lines_before: self.no_lines_before.or(other.no_lines_before), + lines_after_imports: self.lines_after_imports.or(other.lines_after_imports), + lines_between_types: self.lines_between_types.or(other.lines_between_types), + forced_separate: self.forced_separate.or(other.forced_separate), + section_order: self.section_order.or(other.section_order), + sections: self.sections.or(other.sections), + } + } +} diff --git a/crates/ruff/src/rules/mccabe/settings.rs b/crates/ruff/src/rules/mccabe/settings.rs index 5eeef9e84aef6..66973458e4b47 100644 --- a/crates/ruff/src/rules/mccabe/settings.rs +++ b/crates/ruff/src/rules/mccabe/settings.rs @@ -3,6 +3,8 @@ use ruff_macros::{CacheKey, ConfigurationOptions}; use serde::{Deserialize, Serialize}; +use crate::settings::configuration::CombinePluginOptions; + #[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Default, ConfigurationOptions)] #[serde( deny_unknown_fields, @@ -49,3 +51,11 @@ impl From for Options { } } } + +impl CombinePluginOptions for Options { + fn combine(self, other: Self) -> Self { + Self { + max_complexity: self.max_complexity.or(other.max_complexity), + } + } +} diff --git a/crates/ruff/src/rules/pep8_naming/settings.rs b/crates/ruff/src/rules/pep8_naming/settings.rs index 96fc9926bac8e..04b54ef9af79b 100644 --- a/crates/ruff/src/rules/pep8_naming/settings.rs +++ b/crates/ruff/src/rules/pep8_naming/settings.rs @@ -3,6 +3,8 @@ use ruff_macros::{CacheKey, ConfigurationOptions}; use serde::{Deserialize, Serialize}; +use crate::settings::configuration::CombinePluginOptions; + const IGNORE_NAMES: [&str; 12] = [ "setUp", "tearDown", @@ -105,3 +107,15 @@ impl From for Options { } } } + +impl CombinePluginOptions for Options { + fn combine(self, other: Self) -> Self { + Self { + ignore_names: self.ignore_names.or(other.ignore_names), + classmethod_decorators: self.classmethod_decorators.or(other.classmethod_decorators), + staticmethod_decorators: self + .staticmethod_decorators + .or(other.staticmethod_decorators), + } + } +} diff --git a/crates/ruff/src/rules/pycodestyle/settings.rs b/crates/ruff/src/rules/pycodestyle/settings.rs index dacb7b4cfe25f..84f97b11be907 100644 --- a/crates/ruff/src/rules/pycodestyle/settings.rs +++ b/crates/ruff/src/rules/pycodestyle/settings.rs @@ -3,6 +3,8 @@ use ruff_macros::{CacheKey, ConfigurationOptions}; use serde::{Deserialize, Serialize}; +use crate::settings::configuration::CombinePluginOptions; + #[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Default, ConfigurationOptions)] #[serde(deny_unknown_fields, rename_all = "kebab-case", rename = "Pycodestyle")] #[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] @@ -55,3 +57,14 @@ impl From for Options { } } } + +impl CombinePluginOptions for Options { + fn combine(self, other: Self) -> Self { + Self { + max_doc_length: self.max_doc_length.or(other.max_doc_length), + ignore_overlong_task_comments: self + .ignore_overlong_task_comments + .or(other.ignore_overlong_task_comments), + } + } +} diff --git a/crates/ruff/src/rules/pydocstyle/settings.rs b/crates/ruff/src/rules/pydocstyle/settings.rs index 3094418cda9f8..25aee2a16af7d 100644 --- a/crates/ruff/src/rules/pydocstyle/settings.rs +++ b/crates/ruff/src/rules/pydocstyle/settings.rs @@ -1,6 +1,6 @@ //! Settings for the `pydocstyle` plugin. -use crate::registry::Rule; +use crate::{registry::Rule, settings::configuration::CombinePluginOptions}; use ruff_macros::{CacheKey, ConfigurationOptions}; use serde::{Deserialize, Serialize}; use std::collections::BTreeSet; @@ -137,3 +137,13 @@ impl From for Options { } } } + +impl CombinePluginOptions for Options { + fn combine(self, other: Self) -> Self { + Self { + convention: self.convention.or(other.convention), + ignore_decorators: self.ignore_decorators.or(other.ignore_decorators), + property_decorators: self.property_decorators.or(other.property_decorators), + } + } +} diff --git a/crates/ruff/src/rules/pylint/settings.rs b/crates/ruff/src/rules/pylint/settings.rs index 61815ffb03709..9d165d3b5afa0 100644 --- a/crates/ruff/src/rules/pylint/settings.rs +++ b/crates/ruff/src/rules/pylint/settings.rs @@ -5,6 +5,8 @@ use ruff_macros::{CacheKey, ConfigurationOptions}; use rustpython_parser::ast::Constant; use serde::{Deserialize, Serialize}; +use crate::settings::configuration::CombinePluginOptions; + #[derive(Debug, Copy, Clone, PartialEq, Eq, Serialize, Deserialize, CacheKey)] #[serde(deny_unknown_fields, rename_all = "kebab-case")] #[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] @@ -117,3 +119,17 @@ impl From for Options { } } } + +impl CombinePluginOptions for Options { + fn combine(self, other: Self) -> Self { + Self { + allow_magic_value_types: self + .allow_magic_value_types + .or(other.allow_magic_value_types), + max_branches: self.max_branches.or(other.max_branches), + max_returns: self.max_returns.or(other.max_returns), + max_args: self.max_args.or(other.max_args), + max_statements: self.max_statements.or(other.max_statements), + } + } +} diff --git a/crates/ruff/src/settings/configuration.rs b/crates/ruff/src/settings/configuration.rs index d212277287ac2..612122cefc5aa 100644 --- a/crates/ruff/src/settings/configuration.rs +++ b/crates/ruff/src/settings/configuration.rs @@ -268,33 +268,37 @@ impl Configuration { task_tags: self.task_tags.or(config.task_tags), typing_modules: self.typing_modules.or(config.typing_modules), // Plugins - flake8_annotations: self.flake8_annotations.or(config.flake8_annotations), - flake8_bandit: self.flake8_bandit.or(config.flake8_bandit), - flake8_bugbear: self.flake8_bugbear.or(config.flake8_bugbear), - flake8_builtins: self.flake8_builtins.or(config.flake8_builtins), - flake8_comprehensions: self.flake8_comprehensions.or(config.flake8_comprehensions), - flake8_errmsg: self.flake8_errmsg.or(config.flake8_errmsg), - flake8_gettext: self.flake8_gettext.or(config.flake8_gettext), + flake8_annotations: self.flake8_annotations.combine(config.flake8_annotations), + flake8_bandit: self.flake8_bandit.combine(config.flake8_bandit), + flake8_bugbear: self.flake8_bugbear.combine(config.flake8_bugbear), + flake8_builtins: self.flake8_builtins.combine(config.flake8_builtins), + flake8_comprehensions: self + .flake8_comprehensions + .combine(config.flake8_comprehensions), + flake8_errmsg: self.flake8_errmsg.combine(config.flake8_errmsg), + flake8_gettext: self.flake8_gettext.combine(config.flake8_gettext), flake8_implicit_str_concat: self .flake8_implicit_str_concat - .or(config.flake8_implicit_str_concat), + .combine(config.flake8_implicit_str_concat), flake8_import_conventions: self .flake8_import_conventions - .or(config.flake8_import_conventions), - flake8_pytest_style: self.flake8_pytest_style.or(config.flake8_pytest_style), - flake8_quotes: self.flake8_quotes.or(config.flake8_quotes), - flake8_self: self.flake8_self.or(config.flake8_self), - flake8_tidy_imports: self.flake8_tidy_imports.or(config.flake8_tidy_imports), - flake8_type_checking: self.flake8_type_checking.or(config.flake8_type_checking), + .combine(config.flake8_import_conventions), + flake8_pytest_style: self.flake8_pytest_style.combine(config.flake8_pytest_style), + flake8_quotes: self.flake8_quotes.combine(config.flake8_quotes), + flake8_self: self.flake8_self.combine(config.flake8_self), + flake8_tidy_imports: self.flake8_tidy_imports.combine(config.flake8_tidy_imports), + flake8_type_checking: self + .flake8_type_checking + .combine(config.flake8_type_checking), flake8_unused_arguments: self .flake8_unused_arguments - .or(config.flake8_unused_arguments), - isort: self.isort.or(config.isort), - mccabe: self.mccabe.or(config.mccabe), - pep8_naming: self.pep8_naming.or(config.pep8_naming), - pycodestyle: self.pycodestyle.or(config.pycodestyle), - pydocstyle: self.pydocstyle.or(config.pydocstyle), - pylint: self.pylint.or(config.pylint), + .combine(config.flake8_unused_arguments), + isort: self.isort.combine(config.isort), + mccabe: self.mccabe.combine(config.mccabe), + pep8_naming: self.pep8_naming.combine(config.pep8_naming), + pycodestyle: self.pycodestyle.combine(config.pycodestyle), + pydocstyle: self.pydocstyle.combine(config.pydocstyle), + pylint: self.pylint.combine(config.pylint), } } } From c8099eac66b2fe8a8c7ac947b30d76114797c93d Mon Sep 17 00:00:00 2001 From: Ben Doerry Date: Sun, 14 May 2023 22:32:51 +0100 Subject: [PATCH 3/5] Implement derive macro for `CombinePluginOptions` Most `CombinePluginOptions` impls are just calling ```rust self.field.or(other.field) ``` for each field in the struct. --- crates/ruff_macros/src/combine_options.rs | 67 +++++++++++++++++++++++ crates/ruff_macros/src/lib.rs | 10 ++++ 2 files changed, 77 insertions(+) create mode 100644 crates/ruff_macros/src/combine_options.rs diff --git a/crates/ruff_macros/src/combine_options.rs b/crates/ruff_macros/src/combine_options.rs new file mode 100644 index 0000000000000..f3a754d1b28cc --- /dev/null +++ b/crates/ruff_macros/src/combine_options.rs @@ -0,0 +1,67 @@ +use quote::{quote, quote_spanned}; +use syn::{Data, DataStruct, DeriveInput, Field, Fields, Path, PathSegment, Type, TypePath}; + +pub(crate) fn derive_impl(input: DeriveInput) -> syn::Result { + let DeriveInput { ident, data, .. } = input; + + match data { + Data::Struct(DataStruct { + fields: Fields::Named(fields), + .. + }) => { + let output = fields + .named + .iter() + .map(handle_field) + .collect::, _>>()?; + + Ok(quote! { + use crate::settings::configuration::CombinePluginOptions; + + impl CombinePluginOptions for #ident { + fn combine(self, other: Self) -> Self { + Self { + #( + #output + ),* + } + } + } + }) + } + _ => Err(syn::Error::new( + ident.span(), + "Can only derive CombineOptions from structs with named fields.", + )), + } +} + +fn handle_field(field: &Field) -> syn::Result { + let ident = field + .ident + .as_ref() + .expect("Expected to handle named fields"); + + match &field.ty { + Type::Path(TypePath { + path: Path { segments, .. }, + .. + }) => match segments.first() { + Some(PathSegment { + ident: type_ident, .. + }) if type_ident == "Option" => Ok(quote_spanned!( + ident.span() => #ident: self.#ident.or(other.#ident) + )), + Some(PathSegment { + ident: type_ident, .. + }) if type_ident == "Vec" => Ok(quote_spanned!( + ident.span() => #ident: other.#ident.into_iter().chain(self.#ident.into_iter()).collect() + )), + _ => Err(syn::Error::new( + ident.span(), + "Expected `Option<_>` or `Vec<_>` as type.", + )), + }, + _ => Err(syn::Error::new(ident.span(), "Expected type.")), + } +} diff --git a/crates/ruff_macros/src/lib.rs b/crates/ruff_macros/src/lib.rs index 8234653419d3b..3c108bf2bc392 100644 --- a/crates/ruff_macros/src/lib.rs +++ b/crates/ruff_macros/src/lib.rs @@ -5,6 +5,7 @@ use proc_macro::TokenStream; use syn::{parse_macro_input, DeriveInput, ItemFn, ItemStruct}; mod cache_key; +mod combine_options; mod config; mod derive_message_formats; mod map_codes; @@ -22,6 +23,15 @@ pub fn derive_config(input: proc_macro::TokenStream) -> proc_macro::TokenStream .into() } +#[proc_macro_derive(CombineOptions)] +pub fn derive_combine_options(input: proc_macro::TokenStream) -> proc_macro::TokenStream { + let input = parse_macro_input!(input as DeriveInput); + + combine_options::derive_impl(input) + .unwrap_or_else(syn::Error::into_compile_error) + .into() +} + #[proc_macro_derive(CacheKey)] pub fn cache_key(input: TokenStream) -> TokenStream { let item = parse_macro_input!(input as DeriveInput); From 753a307d42675f7900330960a02b64acd5ec0b45 Mon Sep 17 00:00:00 2001 From: Ben Doerry Date: Sun, 14 May 2023 22:51:02 +0100 Subject: [PATCH 4/5] Derive `CombinePluginOptions` impls --- .../src/rules/flake8_annotations/settings.rs | 23 ++--------- .../ruff/src/rules/flake8_bandit/settings.rs | 22 ++-------- .../ruff/src/rules/flake8_bugbear/settings.rs | 16 ++------ .../src/rules/flake8_builtins/settings.rs | 16 ++------ .../rules/flake8_comprehensions/settings.rs | 18 ++------ .../ruff/src/rules/flake8_errmsg/settings.rs | 16 ++------ .../ruff/src/rules/flake8_gettext/settings.rs | 21 ++-------- .../flake8_implicit_str_concat/settings.rs | 16 ++------ .../flake8_import_conventions/settings.rs | 19 ++------- .../src/rules/flake8_pytest_style/settings.rs | 30 ++------------ .../ruff/src/rules/flake8_quotes/settings.rs | 19 ++------- crates/ruff/src/rules/flake8_self/settings.rs | 15 ++----- .../src/rules/flake8_tidy_imports/options.rs | 17 ++------ .../rules/flake8_type_checking/settings.rs | 23 ++--------- .../rules/flake8_unused_arguments/settings.rs | 15 ++----- crates/ruff/src/rules/isort/settings.rs | 41 ++----------------- crates/ruff/src/rules/mccabe/settings.rs | 16 ++------ crates/ruff/src/rules/pep8_naming/settings.rs | 20 ++------- crates/ruff/src/rules/pycodestyle/settings.rs | 19 ++------- crates/ruff/src/rules/pydocstyle/settings.rs | 18 +++----- crates/ruff/src/rules/pylint/settings.rs | 22 ++-------- 21 files changed, 85 insertions(+), 337 deletions(-) diff --git a/crates/ruff/src/rules/flake8_annotations/settings.rs b/crates/ruff/src/rules/flake8_annotations/settings.rs index 6eaa39257920a..a0c3ba5115e66 100644 --- a/crates/ruff/src/rules/flake8_annotations/settings.rs +++ b/crates/ruff/src/rules/flake8_annotations/settings.rs @@ -2,12 +2,11 @@ use serde::{Deserialize, Serialize}; -use ruff_macros::CacheKey; -use ruff_macros::ConfigurationOptions; +use ruff_macros::{CacheKey, CombineOptions, ConfigurationOptions}; -use crate::settings::configuration::CombinePluginOptions; - -#[derive(Debug, PartialEq, Eq, Default, Serialize, Deserialize, ConfigurationOptions)] +#[derive( + Debug, PartialEq, Eq, Default, Serialize, Deserialize, ConfigurationOptions, CombineOptions, +)] #[serde( deny_unknown_fields, rename_all = "kebab-case", @@ -95,17 +94,3 @@ impl From for Options { } } } - -impl CombinePluginOptions for Options { - fn combine(self, other: Self) -> Self { - Self { - mypy_init_return: self.mypy_init_return.or(other.mypy_init_return), - suppress_dummy_args: self.suppress_dummy_args.or(other.suppress_dummy_args), - suppress_none_returning: self - .suppress_none_returning - .or(other.suppress_none_returning), - allow_star_arg_any: self.allow_star_arg_any.or(other.allow_star_arg_any), - ignore_fully_untyped: self.ignore_fully_untyped.or(other.ignore_fully_untyped), - } - } -} diff --git a/crates/ruff/src/rules/flake8_bandit/settings.rs b/crates/ruff/src/rules/flake8_bandit/settings.rs index 595c2d866c7e0..76f363edc4645 100644 --- a/crates/ruff/src/rules/flake8_bandit/settings.rs +++ b/crates/ruff/src/rules/flake8_bandit/settings.rs @@ -2,9 +2,7 @@ use serde::{Deserialize, Serialize}; -use ruff_macros::{CacheKey, ConfigurationOptions}; - -use crate::settings::configuration::CombinePluginOptions; +use ruff_macros::{CacheKey, CombineOptions, ConfigurationOptions}; fn default_tmp_dirs() -> Vec { ["/tmp", "/var/tmp", "/dev/shm"] @@ -12,7 +10,9 @@ fn default_tmp_dirs() -> Vec { .to_vec() } -#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Default, ConfigurationOptions)] +#[derive( + Debug, PartialEq, Eq, Serialize, Deserialize, Default, ConfigurationOptions, CombineOptions, +)] #[serde( deny_unknown_fields, rename_all = "kebab-case", @@ -89,17 +89,3 @@ impl Default for Settings { } } } - -impl CombinePluginOptions for Options { - fn combine(self, other: Self) -> Self { - Self { - hardcoded_tmp_directory: self - .hardcoded_tmp_directory - .or(other.hardcoded_tmp_directory), - hardcoded_tmp_directory_extend: self - .hardcoded_tmp_directory_extend - .or(other.hardcoded_tmp_directory_extend), - check_typed_exception: self.check_typed_exception.or(other.check_typed_exception), - } - } -} diff --git a/crates/ruff/src/rules/flake8_bugbear/settings.rs b/crates/ruff/src/rules/flake8_bugbear/settings.rs index d4e8eb778e1c2..4de75f4d782b4 100644 --- a/crates/ruff/src/rules/flake8_bugbear/settings.rs +++ b/crates/ruff/src/rules/flake8_bugbear/settings.rs @@ -2,11 +2,11 @@ use serde::{Deserialize, Serialize}; -use ruff_macros::{CacheKey, ConfigurationOptions}; +use ruff_macros::{CacheKey, CombineOptions, ConfigurationOptions}; -use crate::settings::configuration::CombinePluginOptions; - -#[derive(Debug, PartialEq, Eq, Default, Serialize, Deserialize, ConfigurationOptions)] +#[derive( + Debug, PartialEq, Eq, Default, Serialize, Deserialize, ConfigurationOptions, CombineOptions, +)] #[serde( deny_unknown_fields, rename_all = "kebab-case", @@ -48,11 +48,3 @@ impl From for Options { } } } - -impl CombinePluginOptions for Options { - fn combine(self, other: Self) -> Self { - Self { - extend_immutable_calls: self.extend_immutable_calls.or(other.extend_immutable_calls), - } - } -} diff --git a/crates/ruff/src/rules/flake8_builtins/settings.rs b/crates/ruff/src/rules/flake8_builtins/settings.rs index 8546cf41fc4ac..2a512626b9d90 100644 --- a/crates/ruff/src/rules/flake8_builtins/settings.rs +++ b/crates/ruff/src/rules/flake8_builtins/settings.rs @@ -2,11 +2,11 @@ use serde::{Deserialize, Serialize}; -use ruff_macros::{CacheKey, ConfigurationOptions}; +use ruff_macros::{CacheKey, CombineOptions, ConfigurationOptions}; -use crate::settings::configuration::CombinePluginOptions; - -#[derive(Debug, PartialEq, Eq, Default, Serialize, Deserialize, ConfigurationOptions)] +#[derive( + Debug, PartialEq, Eq, Default, Serialize, Deserialize, ConfigurationOptions, CombineOptions, +)] #[serde( deny_unknown_fields, rename_all = "kebab-case", @@ -43,11 +43,3 @@ impl From for Options { } } } - -impl CombinePluginOptions for Options { - fn combine(self, other: Self) -> Self { - Self { - builtins_ignorelist: self.builtins_ignorelist.or(other.builtins_ignorelist), - } - } -} diff --git a/crates/ruff/src/rules/flake8_comprehensions/settings.rs b/crates/ruff/src/rules/flake8_comprehensions/settings.rs index 26cac2c5a743a..bc063f1922193 100644 --- a/crates/ruff/src/rules/flake8_comprehensions/settings.rs +++ b/crates/ruff/src/rules/flake8_comprehensions/settings.rs @@ -2,11 +2,11 @@ use serde::{Deserialize, Serialize}; -use ruff_macros::{CacheKey, ConfigurationOptions}; +use ruff_macros::{CacheKey, CombineOptions, ConfigurationOptions}; -use crate::settings::configuration::CombinePluginOptions; - -#[derive(Debug, PartialEq, Eq, Default, Serialize, Deserialize, ConfigurationOptions)] +#[derive( + Debug, PartialEq, Eq, Default, Serialize, Deserialize, ConfigurationOptions, CombineOptions, +)] #[serde( deny_unknown_fields, rename_all = "kebab-case", @@ -47,13 +47,3 @@ impl From for Options { } } } - -impl CombinePluginOptions for Options { - fn combine(self, other: Self) -> Self { - Self { - allow_dict_calls_with_keyword_arguments: self - .allow_dict_calls_with_keyword_arguments - .or(other.allow_dict_calls_with_keyword_arguments), - } - } -} diff --git a/crates/ruff/src/rules/flake8_errmsg/settings.rs b/crates/ruff/src/rules/flake8_errmsg/settings.rs index 5c5558356e7d3..b3ea044529465 100644 --- a/crates/ruff/src/rules/flake8_errmsg/settings.rs +++ b/crates/ruff/src/rules/flake8_errmsg/settings.rs @@ -2,11 +2,11 @@ use serde::{Deserialize, Serialize}; -use ruff_macros::{CacheKey, ConfigurationOptions}; +use ruff_macros::{CacheKey, CombineOptions, ConfigurationOptions}; -use crate::settings::configuration::CombinePluginOptions; - -#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Default, ConfigurationOptions)] +#[derive( + Debug, PartialEq, Eq, Serialize, Deserialize, Default, ConfigurationOptions, CombineOptions, +)] #[serde( deny_unknown_fields, rename_all = "kebab-case", @@ -39,11 +39,3 @@ impl From for Options { } } } - -impl CombinePluginOptions for Options { - fn combine(self, other: Self) -> Self { - Self { - max_string_length: self.max_string_length.or(other.max_string_length), - } - } -} diff --git a/crates/ruff/src/rules/flake8_gettext/settings.rs b/crates/ruff/src/rules/flake8_gettext/settings.rs index 0deef220fffc2..0795e0eb46cd4 100644 --- a/crates/ruff/src/rules/flake8_gettext/settings.rs +++ b/crates/ruff/src/rules/flake8_gettext/settings.rs @@ -1,10 +1,10 @@ use serde::{Deserialize, Serialize}; -use ruff_macros::{CacheKey, ConfigurationOptions}; +use ruff_macros::{CacheKey, CombineOptions, ConfigurationOptions}; -use crate::settings::configuration::CombinePluginOptions; - -#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Default, ConfigurationOptions)] +#[derive( + Debug, PartialEq, Eq, Serialize, Deserialize, Default, ConfigurationOptions, CombineOptions, +)] #[serde( deny_unknown_fields, rename_all = "kebab-case", @@ -71,16 +71,3 @@ impl From for Options { } } } - -impl CombinePluginOptions for Options { - fn combine(self, other: Self) -> Self { - Self { - function_names: self.function_names.or(other.function_names), - extend_function_names: other - .extend_function_names - .into_iter() - .chain(self.extend_function_names.into_iter()) - .collect(), - } - } -} diff --git a/crates/ruff/src/rules/flake8_implicit_str_concat/settings.rs b/crates/ruff/src/rules/flake8_implicit_str_concat/settings.rs index 3b16f341395d7..4a14f15396d19 100644 --- a/crates/ruff/src/rules/flake8_implicit_str_concat/settings.rs +++ b/crates/ruff/src/rules/flake8_implicit_str_concat/settings.rs @@ -2,11 +2,11 @@ use serde::{Deserialize, Serialize}; -use ruff_macros::{CacheKey, ConfigurationOptions}; +use ruff_macros::{CacheKey, CombineOptions, ConfigurationOptions}; -use crate::settings::configuration::CombinePluginOptions; - -#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Default, ConfigurationOptions)] +#[derive( + Debug, PartialEq, Eq, Serialize, Deserialize, Default, ConfigurationOptions, CombineOptions, +)] #[serde( deny_unknown_fields, rename_all = "kebab-case", @@ -61,11 +61,3 @@ impl From for Options { } } } - -impl CombinePluginOptions for Options { - fn combine(self, other: Self) -> Self { - Self { - allow_multiline: self.allow_multiline.or(other.allow_multiline), - } - } -} diff --git a/crates/ruff/src/rules/flake8_import_conventions/settings.rs b/crates/ruff/src/rules/flake8_import_conventions/settings.rs index 953d50de10723..95a0fb7e2e168 100644 --- a/crates/ruff/src/rules/flake8_import_conventions/settings.rs +++ b/crates/ruff/src/rules/flake8_import_conventions/settings.rs @@ -3,9 +3,7 @@ use rustc_hash::{FxHashMap, FxHashSet}; use serde::{Deserialize, Serialize}; -use ruff_macros::{CacheKey, ConfigurationOptions}; - -use crate::settings::configuration::CombinePluginOptions; +use ruff_macros::{CacheKey, CombineOptions, ConfigurationOptions}; const CONVENTIONAL_ALIASES: &[(&str, &str)] = &[ ("altair", "alt"), @@ -22,7 +20,9 @@ const CONVENTIONAL_ALIASES: &[(&str, &str)] = &[ ("pyarrow", "pa"), ]; -#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Default, ConfigurationOptions)] +#[derive( + Debug, PartialEq, Eq, Serialize, Deserialize, Default, ConfigurationOptions, CombineOptions, +)] #[serde( deny_unknown_fields, rename_all = "kebab-case", @@ -134,14 +134,3 @@ impl From for Options { } } } - -impl CombinePluginOptions for Options { - fn combine(self, other: Self) -> Self { - Self { - aliases: self.aliases.or(other.aliases), - extend_aliases: self.extend_aliases.or(other.extend_aliases), - banned_aliases: self.banned_aliases.or(other.banned_aliases), - banned_from: self.banned_from.or(other.banned_from), - } - } -} diff --git a/crates/ruff/src/rules/flake8_pytest_style/settings.rs b/crates/ruff/src/rules/flake8_pytest_style/settings.rs index ce30b99db1868..e70545c6d4f9a 100644 --- a/crates/ruff/src/rules/flake8_pytest_style/settings.rs +++ b/crates/ruff/src/rules/flake8_pytest_style/settings.rs @@ -2,9 +2,7 @@ use serde::{Deserialize, Serialize}; -use ruff_macros::{CacheKey, ConfigurationOptions}; - -use crate::settings::configuration::CombinePluginOptions; +use ruff_macros::{CacheKey, CombineOptions, ConfigurationOptions}; use super::types; @@ -22,7 +20,9 @@ fn default_broad_exceptions() -> Vec { .to_vec() } -#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Default, ConfigurationOptions)] +#[derive( + Debug, PartialEq, Eq, Serialize, Deserialize, Default, ConfigurationOptions, CombineOptions, +)] #[serde( deny_unknown_fields, rename_all = "kebab-case", @@ -169,25 +169,3 @@ impl Default for Settings { } } } - -impl CombinePluginOptions for Options { - fn combine(self, other: Self) -> Self { - Self { - fixture_parentheses: self.fixture_parentheses.or(other.fixture_parentheses), - parametrize_names_type: self.parametrize_names_type.or(other.parametrize_names_type), - parametrize_values_type: self - .parametrize_values_type - .or(other.parametrize_values_type), - parametrize_values_row_type: self - .parametrize_values_row_type - .or(other.parametrize_values_row_type), - raises_require_match_for: self - .raises_require_match_for - .or(other.raises_require_match_for), - raises_extend_require_match_for: self - .raises_extend_require_match_for - .or(other.raises_extend_require_match_for), - mark_parentheses: self.mark_parentheses.or(other.mark_parentheses), - } - } -} diff --git a/crates/ruff/src/rules/flake8_quotes/settings.rs b/crates/ruff/src/rules/flake8_quotes/settings.rs index c3781ec99dacb..121501065e90b 100644 --- a/crates/ruff/src/rules/flake8_quotes/settings.rs +++ b/crates/ruff/src/rules/flake8_quotes/settings.rs @@ -2,9 +2,7 @@ use serde::{Deserialize, Serialize}; -use ruff_macros::{CacheKey, ConfigurationOptions}; - -use crate::settings::configuration::CombinePluginOptions; +use ruff_macros::{CacheKey, CombineOptions, ConfigurationOptions}; #[derive(Debug, Copy, Clone, PartialEq, Eq, Serialize, Deserialize, CacheKey)] #[serde(deny_unknown_fields, rename_all = "kebab-case")] @@ -22,7 +20,9 @@ impl Default for Quote { } } -#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Default, ConfigurationOptions)] +#[derive( + Debug, PartialEq, Eq, Serialize, Deserialize, Default, ConfigurationOptions, CombineOptions, +)] #[serde( deny_unknown_fields, rename_all = "kebab-case", @@ -113,14 +113,3 @@ impl From for Options { } } } - -impl CombinePluginOptions for Options { - fn combine(self, other: Self) -> Self { - Self { - inline_quotes: self.inline_quotes.or(other.inline_quotes), - multiline_quotes: self.multiline_quotes.or(other.multiline_quotes), - docstring_quotes: self.docstring_quotes.or(other.docstring_quotes), - avoid_escape: self.avoid_escape.or(other.avoid_escape), - } - } -} diff --git a/crates/ruff/src/rules/flake8_self/settings.rs b/crates/ruff/src/rules/flake8_self/settings.rs index 248ddac8a2c2c..728b64c97c286 100644 --- a/crates/ruff/src/rules/flake8_self/settings.rs +++ b/crates/ruff/src/rules/flake8_self/settings.rs @@ -2,15 +2,15 @@ use serde::{Deserialize, Serialize}; -use ruff_macros::{CacheKey, ConfigurationOptions}; - -use crate::settings::configuration::CombinePluginOptions; +use ruff_macros::{CacheKey, CombineOptions, ConfigurationOptions}; // By default, ignore the `namedtuple` methods and attributes, which are underscore-prefixed to // prevent conflicts with field names. const IGNORE_NAMES: [&str; 5] = ["_make", "_asdict", "_replace", "_fields", "_field_defaults"]; -#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Default, ConfigurationOptions)] +#[derive( + Debug, PartialEq, Eq, Serialize, Deserialize, Default, ConfigurationOptions, CombineOptions, +)] #[serde( deny_unknown_fields, rename_all = "kebab-case", @@ -59,10 +59,3 @@ impl From for Options { } } } -impl CombinePluginOptions for Options { - fn combine(self, other: Self) -> Self { - Self { - ignore_names: self.ignore_names.or(other.ignore_names), - } - } -} diff --git a/crates/ruff/src/rules/flake8_tidy_imports/options.rs b/crates/ruff/src/rules/flake8_tidy_imports/options.rs index e99225e81bd2c..f180974a49776 100644 --- a/crates/ruff/src/rules/flake8_tidy_imports/options.rs +++ b/crates/ruff/src/rules/flake8_tidy_imports/options.rs @@ -3,15 +3,15 @@ use rustc_hash::FxHashMap; use serde::{Deserialize, Serialize}; -use ruff_macros::ConfigurationOptions; - -use crate::settings::configuration::CombinePluginOptions; +use ruff_macros::{CombineOptions, ConfigurationOptions}; use super::banned_api::ApiBan; use super::relative_imports::Strictness; use super::Settings; -#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Default, ConfigurationOptions)] +#[derive( + Debug, PartialEq, Eq, Serialize, Deserialize, Default, ConfigurationOptions, CombineOptions, +)] #[serde( deny_unknown_fields, rename_all = "kebab-case", @@ -62,12 +62,3 @@ impl From for Options { } } } - -impl CombinePluginOptions for Options { - fn combine(self, other: Self) -> Self { - Self { - ban_relative_imports: self.ban_relative_imports.or(other.ban_relative_imports), - banned_api: self.banned_api.or(other.banned_api), - } - } -} diff --git a/crates/ruff/src/rules/flake8_type_checking/settings.rs b/crates/ruff/src/rules/flake8_type_checking/settings.rs index e5112ba110faf..69cd913dae913 100644 --- a/crates/ruff/src/rules/flake8_type_checking/settings.rs +++ b/crates/ruff/src/rules/flake8_type_checking/settings.rs @@ -2,11 +2,11 @@ use serde::{Deserialize, Serialize}; -use ruff_macros::{CacheKey, ConfigurationOptions}; +use ruff_macros::{CacheKey, CombineOptions, ConfigurationOptions}; -use crate::settings::configuration::CombinePluginOptions; - -#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Default, ConfigurationOptions)] +#[derive( + Debug, PartialEq, Eq, Serialize, Deserialize, Default, ConfigurationOptions, CombineOptions, +)] #[serde( deny_unknown_fields, rename_all = "kebab-case", @@ -101,18 +101,3 @@ impl From for Options { } } } - -impl CombinePluginOptions for Options { - fn combine(self, other: Self) -> Self { - Self { - strict: self.strict.or(other.strict), - exempt_modules: self.exempt_modules.or(other.exempt_modules), - runtime_evaluated_base_classes: self - .runtime_evaluated_base_classes - .or(other.runtime_evaluated_base_classes), - runtime_evaluated_decorators: self - .runtime_evaluated_decorators - .or(other.runtime_evaluated_decorators), - } - } -} diff --git a/crates/ruff/src/rules/flake8_unused_arguments/settings.rs b/crates/ruff/src/rules/flake8_unused_arguments/settings.rs index 8b4f8e47ed388..f8853ddab47be 100644 --- a/crates/ruff/src/rules/flake8_unused_arguments/settings.rs +++ b/crates/ruff/src/rules/flake8_unused_arguments/settings.rs @@ -2,11 +2,11 @@ use serde::{Deserialize, Serialize}; -use ruff_macros::{CacheKey, ConfigurationOptions}; +use ruff_macros::{CacheKey, CombineOptions, ConfigurationOptions}; -use crate::settings::configuration::CombinePluginOptions; - -#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Default, ConfigurationOptions)] +#[derive( + Debug, PartialEq, Eq, Serialize, Deserialize, Default, ConfigurationOptions, CombineOptions, +)] #[serde( deny_unknown_fields, rename_all = "kebab-case", @@ -43,10 +43,3 @@ impl From for Options { } } } -impl CombinePluginOptions for Options { - fn combine(self, other: Self) -> Self { - Self { - ignore_variadic_names: self.ignore_variadic_names.or(other.ignore_variadic_names), - } - } -} diff --git a/crates/ruff/src/rules/isort/settings.rs b/crates/ruff/src/rules/isort/settings.rs index 8bbf1d16682cb..262829b73d425 100644 --- a/crates/ruff/src/rules/isort/settings.rs +++ b/crates/ruff/src/rules/isort/settings.rs @@ -7,11 +7,10 @@ use rustc_hash::{FxHashMap, FxHashSet}; use serde::{Deserialize, Serialize}; use strum::IntoEnumIterator; -use ruff_macros::{CacheKey, ConfigurationOptions}; +use ruff_macros::{CacheKey, CombineOptions, ConfigurationOptions}; use crate::rules::isort::categorize::KnownModules; use crate::rules::isort::ImportType; -use crate::settings::configuration::CombinePluginOptions; use crate::warn_user_once; use super::categorize::ImportSection; @@ -34,7 +33,9 @@ impl Default for RelativeImportsOrder { } } -#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Default, ConfigurationOptions)] +#[derive( + Debug, PartialEq, Eq, Serialize, Deserialize, Default, ConfigurationOptions, CombineOptions, +)] #[serde( deny_unknown_fields, rename_all = "kebab-case", @@ -507,37 +508,3 @@ impl From for Options { } } } - -impl CombinePluginOptions for Options { - fn combine(self, other: Self) -> Self { - Self { - force_wrap_aliases: self.force_wrap_aliases.or(other.force_wrap_aliases), - force_single_line: self.force_single_line.or(other.force_single_line), - single_line_exclusions: self.single_line_exclusions.or(other.single_line_exclusions), - combine_as_imports: self.combine_as_imports.or(other.combine_as_imports), - split_on_trailing_comma: self - .split_on_trailing_comma - .or(other.split_on_trailing_comma), - order_by_type: self.order_by_type.or(other.order_by_type), - force_sort_within_sections: self - .force_sort_within_sections - .or(other.force_sort_within_sections), - force_to_top: self.force_to_top.or(other.force_to_top), - known_first_party: self.known_first_party.or(other.known_first_party), - known_third_party: self.known_third_party.or(other.known_third_party), - known_local_folder: self.known_local_folder.or(other.known_local_folder), - extra_standard_library: self.extra_standard_library.or(other.extra_standard_library), - relative_imports_order: self.relative_imports_order.or(other.relative_imports_order), - required_imports: self.required_imports.or(other.required_imports), - classes: self.classes.or(other.classes), - constants: self.constants.or(other.constants), - variables: self.variables.or(other.variables), - no_lines_before: self.no_lines_before.or(other.no_lines_before), - lines_after_imports: self.lines_after_imports.or(other.lines_after_imports), - lines_between_types: self.lines_between_types.or(other.lines_between_types), - forced_separate: self.forced_separate.or(other.forced_separate), - section_order: self.section_order.or(other.section_order), - sections: self.sections.or(other.sections), - } - } -} diff --git a/crates/ruff/src/rules/mccabe/settings.rs b/crates/ruff/src/rules/mccabe/settings.rs index 66973458e4b47..8f53f535e2e6b 100644 --- a/crates/ruff/src/rules/mccabe/settings.rs +++ b/crates/ruff/src/rules/mccabe/settings.rs @@ -1,11 +1,11 @@ //! Settings for the `mccabe` plugin. -use ruff_macros::{CacheKey, ConfigurationOptions}; +use ruff_macros::{CacheKey, CombineOptions, ConfigurationOptions}; use serde::{Deserialize, Serialize}; -use crate::settings::configuration::CombinePluginOptions; - -#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Default, ConfigurationOptions)] +#[derive( + Debug, PartialEq, Eq, Serialize, Deserialize, Default, ConfigurationOptions, CombineOptions, +)] #[serde( deny_unknown_fields, rename_all = "kebab-case", @@ -51,11 +51,3 @@ impl From for Options { } } } - -impl CombinePluginOptions for Options { - fn combine(self, other: Self) -> Self { - Self { - max_complexity: self.max_complexity.or(other.max_complexity), - } - } -} diff --git a/crates/ruff/src/rules/pep8_naming/settings.rs b/crates/ruff/src/rules/pep8_naming/settings.rs index 04b54ef9af79b..ffc77699fc0d8 100644 --- a/crates/ruff/src/rules/pep8_naming/settings.rs +++ b/crates/ruff/src/rules/pep8_naming/settings.rs @@ -1,10 +1,8 @@ //! Settings for the `pep8-naming` plugin. -use ruff_macros::{CacheKey, ConfigurationOptions}; +use ruff_macros::{CacheKey, CombineOptions, ConfigurationOptions}; use serde::{Deserialize, Serialize}; -use crate::settings::configuration::CombinePluginOptions; - const IGNORE_NAMES: [&str; 12] = [ "setUp", "tearDown", @@ -20,7 +18,9 @@ const IGNORE_NAMES: [&str; 12] = [ "maxDiff", ]; -#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Default, ConfigurationOptions)] +#[derive( + Debug, PartialEq, Eq, Serialize, Deserialize, Default, ConfigurationOptions, CombineOptions, +)] #[serde( deny_unknown_fields, rename_all = "kebab-case", @@ -107,15 +107,3 @@ impl From for Options { } } } - -impl CombinePluginOptions for Options { - fn combine(self, other: Self) -> Self { - Self { - ignore_names: self.ignore_names.or(other.ignore_names), - classmethod_decorators: self.classmethod_decorators.or(other.classmethod_decorators), - staticmethod_decorators: self - .staticmethod_decorators - .or(other.staticmethod_decorators), - } - } -} diff --git a/crates/ruff/src/rules/pycodestyle/settings.rs b/crates/ruff/src/rules/pycodestyle/settings.rs index 84f97b11be907..06204f29bba3c 100644 --- a/crates/ruff/src/rules/pycodestyle/settings.rs +++ b/crates/ruff/src/rules/pycodestyle/settings.rs @@ -1,11 +1,11 @@ //! Settings for the `pycodestyle` plugin. -use ruff_macros::{CacheKey, ConfigurationOptions}; +use ruff_macros::{CacheKey, CombineOptions, ConfigurationOptions}; use serde::{Deserialize, Serialize}; -use crate::settings::configuration::CombinePluginOptions; - -#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Default, ConfigurationOptions)] +#[derive( + Debug, PartialEq, Eq, Serialize, Deserialize, Default, ConfigurationOptions, CombineOptions, +)] #[serde(deny_unknown_fields, rename_all = "kebab-case", rename = "Pycodestyle")] #[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] pub struct Options { @@ -57,14 +57,3 @@ impl From for Options { } } } - -impl CombinePluginOptions for Options { - fn combine(self, other: Self) -> Self { - Self { - max_doc_length: self.max_doc_length.or(other.max_doc_length), - ignore_overlong_task_comments: self - .ignore_overlong_task_comments - .or(other.ignore_overlong_task_comments), - } - } -} diff --git a/crates/ruff/src/rules/pydocstyle/settings.rs b/crates/ruff/src/rules/pydocstyle/settings.rs index 25aee2a16af7d..e3ce5bd399f8f 100644 --- a/crates/ruff/src/rules/pydocstyle/settings.rs +++ b/crates/ruff/src/rules/pydocstyle/settings.rs @@ -1,7 +1,7 @@ //! Settings for the `pydocstyle` plugin. -use crate::{registry::Rule, settings::configuration::CombinePluginOptions}; -use ruff_macros::{CacheKey, ConfigurationOptions}; +use crate::registry::Rule; +use ruff_macros::{CacheKey, CombineOptions, ConfigurationOptions}; use serde::{Deserialize, Serialize}; use std::collections::BTreeSet; @@ -68,7 +68,9 @@ impl Convention { } } -#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Default, ConfigurationOptions)] +#[derive( + Debug, PartialEq, Eq, Serialize, Deserialize, Default, ConfigurationOptions, CombineOptions, +)] #[serde(deny_unknown_fields, rename_all = "kebab-case", rename = "Pydocstyle")] #[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] pub struct Options { @@ -137,13 +139,3 @@ impl From for Options { } } } - -impl CombinePluginOptions for Options { - fn combine(self, other: Self) -> Self { - Self { - convention: self.convention.or(other.convention), - ignore_decorators: self.ignore_decorators.or(other.ignore_decorators), - property_decorators: self.property_decorators.or(other.property_decorators), - } - } -} diff --git a/crates/ruff/src/rules/pylint/settings.rs b/crates/ruff/src/rules/pylint/settings.rs index 9d165d3b5afa0..2525d2f564ea2 100644 --- a/crates/ruff/src/rules/pylint/settings.rs +++ b/crates/ruff/src/rules/pylint/settings.rs @@ -1,12 +1,10 @@ //! Settings for the `pylint` plugin. use anyhow::anyhow; -use ruff_macros::{CacheKey, ConfigurationOptions}; +use ruff_macros::{CacheKey, CombineOptions, ConfigurationOptions}; use rustpython_parser::ast::Constant; use serde::{Deserialize, Serialize}; -use crate::settings::configuration::CombinePluginOptions; - #[derive(Debug, Copy, Clone, PartialEq, Eq, Serialize, Deserialize, CacheKey)] #[serde(deny_unknown_fields, rename_all = "kebab-case")] #[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] @@ -37,7 +35,9 @@ impl TryFrom<&Constant> for ConstantType { } } -#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Default, ConfigurationOptions)] +#[derive( + Debug, PartialEq, Eq, Serialize, Deserialize, Default, ConfigurationOptions, CombineOptions, +)] #[serde( deny_unknown_fields, rename_all = "kebab-case", @@ -119,17 +119,3 @@ impl From for Options { } } } - -impl CombinePluginOptions for Options { - fn combine(self, other: Self) -> Self { - Self { - allow_magic_value_types: self - .allow_magic_value_types - .or(other.allow_magic_value_types), - max_branches: self.max_branches.or(other.max_branches), - max_returns: self.max_returns.or(other.max_returns), - max_args: self.max_args.or(other.max_args), - max_statements: self.max_statements.or(other.max_statements), - } - } -} From 1bb6c8cbb82f7619fa925ad686a097494c66b321 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Sun, 14 May 2023 22:22:33 -0400 Subject: [PATCH 5/5] Remove vec --- crates/ruff_macros/src/combine_options.rs | 5 ----- 1 file changed, 5 deletions(-) diff --git a/crates/ruff_macros/src/combine_options.rs b/crates/ruff_macros/src/combine_options.rs index f3a754d1b28cc..8f7e381093e65 100644 --- a/crates/ruff_macros/src/combine_options.rs +++ b/crates/ruff_macros/src/combine_options.rs @@ -52,11 +52,6 @@ fn handle_field(field: &Field) -> syn::Result { }) if type_ident == "Option" => Ok(quote_spanned!( ident.span() => #ident: self.#ident.or(other.#ident) )), - Some(PathSegment { - ident: type_ident, .. - }) if type_ident == "Vec" => Ok(quote_spanned!( - ident.span() => #ident: other.#ident.into_iter().chain(self.#ident.into_iter()).collect() - )), _ => Err(syn::Error::new( ident.span(), "Expected `Option<_>` or `Vec<_>` as type.",