Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add explicit-preview-rules to toggle explicit selection of preview rules #7390

Merged
merged 1 commit into from Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions crates/flake8_to_ruff/src/plugin.rs
Expand Up @@ -4,7 +4,7 @@ use std::str::FromStr;

use anyhow::anyhow;
use ruff_linter::registry::Linter;
use ruff_linter::settings::types::PreviewMode;
use ruff_linter::rule_selector::PreviewOptions;
use ruff_linter::RuleSelector;

#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq)]
Expand Down Expand Up @@ -332,7 +332,7 @@ pub(crate) fn infer_plugins_from_codes(selectors: &HashSet<RuleSelector>) -> Vec
.filter(|plugin| {
for selector in selectors {
if selector
.rules(PreviewMode::Disabled)
.rules(&PreviewOptions::default())
.any(|rule| Linter::from(plugin).rules().any(|r| r == rule))
{
return true;
Expand Down
19 changes: 15 additions & 4 deletions crates/ruff_linter/src/rule_selector.rs
Expand Up @@ -198,16 +198,19 @@ impl RuleSelector {
}
}

/// Returns rules matching the selector, taking into account whether preview mode is enabled.
pub fn rules(&self, preview: PreviewMode) -> impl Iterator<Item = Rule> + '_ {
/// Returns rules matching the selector, taking into account preview options enabled.
pub fn rules<'a>(&'a self, preview: &PreviewOptions) -> impl Iterator<Item = Rule> + 'a {
let preview_enabled = preview.mode.is_enabled();
let preview_require_explicit = preview.require_explicit;
#[allow(deprecated)]
self.all_rules().filter(move |rule| {
// Always include rules that are not in preview or the nursery
!(rule.is_preview() || rule.is_nursery())
// Backwards compatibility allows selection of nursery rules by exact code or dedicated group
|| ((matches!(self, RuleSelector::Rule { .. }) || matches!(self, RuleSelector::Nursery { .. })) && rule.is_nursery())
// Enabling preview includes all preview or nursery rules
|| preview.is_enabled()
// Enabling preview includes all preview or nursery rules unless explicit selection
// is turned on
|| (preview_enabled && (matches!(self, RuleSelector::Rule { .. }) || !preview_require_explicit))
})
}
}
Expand All @@ -232,6 +235,14 @@ impl Iterator for RuleSelectorIter {
}
}

#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct PreviewOptions {
pub mode: PreviewMode,
/// If true, preview rule selection requires explicit codes e.g. not prefixes.
/// Generally this should be derived from the user-facing `explicit-preview-rules` option.
pub require_explicit: bool,
zanieb marked this conversation as resolved.
Show resolved Hide resolved
}

#[cfg(feature = "schemars")]
mod schema {
use itertools::Itertools;
Expand Down
5 changes: 4 additions & 1 deletion crates/ruff_linter/src/settings/mod.rs
Expand Up @@ -30,6 +30,7 @@ use super::line_width::{LineLength, TabSize};

use self::rule_table::RuleTable;
use self::types::PreviewMode;
use crate::rule_selector::PreviewOptions;

pub mod flags;
pub mod rule_table;
Expand All @@ -44,6 +45,7 @@ pub struct LinterSettings {

pub target_version: PythonVersion,
pub preview: PreviewMode,
pub explicit_preview_rules: bool,

// Rule-specific settings
pub allowed_confusables: FxHashSet<char>,
Expand Down Expand Up @@ -121,7 +123,7 @@ impl LinterSettings {
project_root: project_root.to_path_buf(),
rules: PREFIXES
.iter()
.flat_map(|selector| selector.rules(PreviewMode::default()))
.flat_map(|selector| selector.rules(&PreviewOptions::default()))
.collect(),
allowed_confusables: FxHashSet::from_iter([]),

Expand Down Expand Up @@ -168,6 +170,7 @@ impl LinterSettings {
pylint: pylint::settings::Settings::default(),
pyupgrade: pyupgrade::settings::Settings::default(),
preview: PreviewMode::default(),
explicit_preview_rules: false,
}
}

Expand Down