mirror of https://github.com/astral-sh/ruff
Error if nursery rules are selected without preview (#9683)
Extends #9682 to error if the nursery selector is used or nursery rules are selected without preview. Part of #7992 — we will remove this in 0.3.0 instead so we can provide nice errors in 0.2.0.
This commit is contained in:
parent
57576fa139
commit
07c6a3e672
|
|
@ -852,14 +852,12 @@ fn nursery_direct() {
|
||||||
assert_cmd_snapshot!(cmd
|
assert_cmd_snapshot!(cmd
|
||||||
.pass_stdin("I=42\n"), @r###"
|
.pass_stdin("I=42\n"), @r###"
|
||||||
success: false
|
success: false
|
||||||
exit_code: 1
|
exit_code: 2
|
||||||
----- stdout -----
|
----- stdout -----
|
||||||
-:1:2: E225 [*] Missing whitespace around operator
|
|
||||||
Found 1 error.
|
|
||||||
[*] 1 fixable with the `--fix` option.
|
|
||||||
|
|
||||||
----- stderr -----
|
----- stderr -----
|
||||||
warning: Selection of nursery rule `E225` without the `--preview` flag is deprecated.
|
ruff failed
|
||||||
|
Cause: Selection of unstable rule `E225` without the `--preview` flag is not allowed.
|
||||||
"###);
|
"###);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -870,15 +868,12 @@ fn nursery_group_selector() {
|
||||||
assert_cmd_snapshot!(cmd
|
assert_cmd_snapshot!(cmd
|
||||||
.pass_stdin("I=42\n"), @r###"
|
.pass_stdin("I=42\n"), @r###"
|
||||||
success: false
|
success: false
|
||||||
exit_code: 1
|
exit_code: 2
|
||||||
----- stdout -----
|
----- stdout -----
|
||||||
-:1:1: CPY001 Missing copyright notice at top of file
|
|
||||||
-:1:2: E225 [*] Missing whitespace around operator
|
|
||||||
Found 2 errors.
|
|
||||||
[*] 1 fixable with the `--fix` option.
|
|
||||||
|
|
||||||
----- stderr -----
|
----- stderr -----
|
||||||
warning: The `NURSERY` selector has been deprecated. Use the `--preview` flag instead.
|
ruff failed
|
||||||
|
Cause: The `NURSERY` selector was removed. Use the `--preview` flag instead.
|
||||||
"###);
|
"###);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -896,7 +891,7 @@ fn nursery_group_selector_preview_enabled() {
|
||||||
|
|
||||||
----- stderr -----
|
----- stderr -----
|
||||||
ruff failed
|
ruff failed
|
||||||
Cause: The `NURSERY` selector is deprecated and cannot be used with preview mode enabled.
|
Cause: The `NURSERY` selector was removed. Unstable rules should be selected individually or by their respective groups.
|
||||||
"###);
|
"###);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -887,10 +887,12 @@ impl LintConfiguration {
|
||||||
for (kind, selector) in selection.selectors_by_kind() {
|
for (kind, selector) in selection.selectors_by_kind() {
|
||||||
#[allow(deprecated)]
|
#[allow(deprecated)]
|
||||||
if matches!(selector, RuleSelector::Nursery) {
|
if matches!(selector, RuleSelector::Nursery) {
|
||||||
if preview.mode.is_enabled() {
|
let suggestion = if preview.mode.is_disabled() {
|
||||||
return Err(anyhow!("The `NURSERY` selector is deprecated and cannot be used with preview mode enabled."));
|
" Use the `--preview` flag instead."
|
||||||
}
|
} else {
|
||||||
warn_user_once!("The `NURSERY` selector has been deprecated. Use the `--preview` flag instead.");
|
" Unstable rules should be selected individually or by their respective groups."
|
||||||
|
};
|
||||||
|
return Err(anyhow!("The `NURSERY` selector was removed.{suggestion}"));
|
||||||
};
|
};
|
||||||
|
|
||||||
// Only warn for the following selectors if used to enable rules
|
// Only warn for the following selectors if used to enable rules
|
||||||
|
|
@ -928,9 +930,24 @@ impl LintConfiguration {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
for selection in deprecated_nursery_selectors {
|
let deprecated_nursery_selectors = deprecated_nursery_selectors.iter().collect::<Vec<_>>();
|
||||||
let (prefix, code) = selection.prefix_and_code();
|
match deprecated_nursery_selectors.as_slice() {
|
||||||
warn_user!("Selection of nursery rule `{prefix}{code}` without the `--preview` flag is deprecated.",);
|
[] => (),
|
||||||
|
[selection] => {
|
||||||
|
let (prefix, code) = selection.prefix_and_code();
|
||||||
|
return Err(anyhow!("Selection of unstable rule `{prefix}{code}` without the `--preview` flag is not allowed."));
|
||||||
|
}
|
||||||
|
[..] => {
|
||||||
|
let mut message = "Selection of unstable rules without the `--preview` flag is not allowed. Enable preview or remove selection of:".to_string();
|
||||||
|
for selection in deprecated_nursery_selectors {
|
||||||
|
let (prefix, code) = selection.prefix_and_code();
|
||||||
|
message.push_str("\n\t- ");
|
||||||
|
message.push_str(prefix);
|
||||||
|
message.push_str(code);
|
||||||
|
}
|
||||||
|
message.push('\n');
|
||||||
|
return Err(anyhow!(message));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for selection in ignored_preview_selectors {
|
for selection in ignored_preview_selectors {
|
||||||
|
|
@ -1363,51 +1380,6 @@ mod tests {
|
||||||
use ruff_linter::RuleSelector;
|
use ruff_linter::RuleSelector;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
|
||||||
const NURSERY_RULES: &[Rule] = &[
|
|
||||||
Rule::MissingCopyrightNotice,
|
|
||||||
Rule::IndentationWithInvalidMultiple,
|
|
||||||
Rule::NoIndentedBlock,
|
|
||||||
Rule::UnexpectedIndentation,
|
|
||||||
Rule::IndentationWithInvalidMultipleComment,
|
|
||||||
Rule::NoIndentedBlockComment,
|
|
||||||
Rule::UnexpectedIndentationComment,
|
|
||||||
Rule::OverIndented,
|
|
||||||
Rule::WhitespaceAfterOpenBracket,
|
|
||||||
Rule::WhitespaceBeforeCloseBracket,
|
|
||||||
Rule::WhitespaceBeforePunctuation,
|
|
||||||
Rule::WhitespaceBeforeParameters,
|
|
||||||
Rule::MultipleSpacesBeforeOperator,
|
|
||||||
Rule::MultipleSpacesAfterOperator,
|
|
||||||
Rule::TabBeforeOperator,
|
|
||||||
Rule::TabAfterOperator,
|
|
||||||
Rule::MissingWhitespaceAroundOperator,
|
|
||||||
Rule::MissingWhitespaceAroundArithmeticOperator,
|
|
||||||
Rule::MissingWhitespaceAroundBitwiseOrShiftOperator,
|
|
||||||
Rule::MissingWhitespaceAroundModuloOperator,
|
|
||||||
Rule::MissingWhitespace,
|
|
||||||
Rule::MultipleSpacesAfterComma,
|
|
||||||
Rule::TabAfterComma,
|
|
||||||
Rule::UnexpectedSpacesAroundKeywordParameterEquals,
|
|
||||||
Rule::MissingWhitespaceAroundParameterEquals,
|
|
||||||
Rule::TooFewSpacesBeforeInlineComment,
|
|
||||||
Rule::NoSpaceAfterInlineComment,
|
|
||||||
Rule::NoSpaceAfterBlockComment,
|
|
||||||
Rule::MultipleLeadingHashesForBlockComment,
|
|
||||||
Rule::MultipleSpacesAfterKeyword,
|
|
||||||
Rule::MultipleSpacesBeforeKeyword,
|
|
||||||
Rule::TabAfterKeyword,
|
|
||||||
Rule::TabBeforeKeyword,
|
|
||||||
Rule::MissingWhitespaceAfterKeyword,
|
|
||||||
Rule::CompareToEmptyString,
|
|
||||||
Rule::NoSelfUse,
|
|
||||||
Rule::EqWithoutHash,
|
|
||||||
Rule::BadDunderMethodName,
|
|
||||||
Rule::RepeatedAppend,
|
|
||||||
Rule::DeleteFullSlice,
|
|
||||||
Rule::CheckAndRemoveFromSet,
|
|
||||||
Rule::QuadraticListSummation,
|
|
||||||
];
|
|
||||||
|
|
||||||
const PREVIEW_RULES: &[Rule] = &[
|
const PREVIEW_RULES: &[Rule] = &[
|
||||||
Rule::AndOrTernary,
|
Rule::AndOrTernary,
|
||||||
Rule::AssignmentInAssert,
|
Rule::AssignmentInAssert,
|
||||||
|
|
@ -1809,9 +1781,8 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn nursery_select_code() -> Result<()> {
|
fn nursery_select_code() -> Result<()> {
|
||||||
// Backwards compatible behavior allows selection of nursery rules with their exact code
|
// We do not allow selection of nursery rules when preview is disabled
|
||||||
// when preview is disabled
|
assert!(resolve_rules(
|
||||||
let actual = resolve_rules(
|
|
||||||
[RuleSelection {
|
[RuleSelection {
|
||||||
select: Some(vec![Flake8Copyright::_001.into()]),
|
select: Some(vec![Flake8Copyright::_001.into()]),
|
||||||
..RuleSelection::default()
|
..RuleSelection::default()
|
||||||
|
|
@ -1820,9 +1791,8 @@ mod tests {
|
||||||
mode: PreviewMode::Disabled,
|
mode: PreviewMode::Disabled,
|
||||||
..PreviewOptions::default()
|
..PreviewOptions::default()
|
||||||
}),
|
}),
|
||||||
)?;
|
)
|
||||||
let expected = RuleSet::from_rule(Rule::MissingCopyrightNotice);
|
.is_err());
|
||||||
assert_eq!(actual, expected);
|
|
||||||
|
|
||||||
let actual = resolve_rules(
|
let actual = resolve_rules(
|
||||||
[RuleSelection {
|
[RuleSelection {
|
||||||
|
|
@ -1841,10 +1811,9 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[allow(deprecated)]
|
#[allow(deprecated)]
|
||||||
fn select_nursery() -> Result<()> {
|
fn select_nursery() {
|
||||||
// Backwards compatible behavior allows selection of nursery rules with the nursery selector
|
// We no longer allow use of the NURSERY selector and should error in both cases
|
||||||
// when preview is disabled
|
assert!(resolve_rules(
|
||||||
let actual = resolve_rules(
|
|
||||||
[RuleSelection {
|
[RuleSelection {
|
||||||
select: Some(vec![RuleSelector::Nursery]),
|
select: Some(vec![RuleSelector::Nursery]),
|
||||||
..RuleSelection::default()
|
..RuleSelection::default()
|
||||||
|
|
@ -1853,11 +1822,8 @@ mod tests {
|
||||||
mode: PreviewMode::Disabled,
|
mode: PreviewMode::Disabled,
|
||||||
..PreviewOptions::default()
|
..PreviewOptions::default()
|
||||||
}),
|
}),
|
||||||
)?;
|
)
|
||||||
let expected = RuleSet::from_rules(NURSERY_RULES);
|
.is_err());
|
||||||
assert_eq!(actual, expected);
|
|
||||||
|
|
||||||
// When preview is enabled, use of NURSERY is banned
|
|
||||||
assert!(resolve_rules(
|
assert!(resolve_rules(
|
||||||
[RuleSelection {
|
[RuleSelection {
|
||||||
select: Some(vec![RuleSelector::Nursery]),
|
select: Some(vec![RuleSelector::Nursery]),
|
||||||
|
|
@ -1869,8 +1835,6 @@ mod tests {
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
.is_err());
|
.is_err());
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
|
||||||
|
|
@ -143,12 +143,3 @@ In our previous example, `--select` with `ALL` `HYP`, `HYP0`, or `HYP00` would n
|
||||||
rule will need to be selected with its exact code, e.g. `--select ALL,HYP001`.
|
rule will need to be selected with its exact code, e.g. `--select ALL,HYP001`.
|
||||||
|
|
||||||
If preview mode is not enabled, this setting has no effect.
|
If preview mode is not enabled, this setting has no effect.
|
||||||
|
|
||||||
## Legacy behavior
|
|
||||||
|
|
||||||
Before the preview mode was introduced, new rules were added in a "nursery" category that required selection of
|
|
||||||
rules with their exact codes — similar to if `explicit-preview-rules` is enabled.
|
|
||||||
|
|
||||||
The nursery category has been deprecated and all rules in the nursery are now considered to be in preview.
|
|
||||||
For backwards compatibility, nursery rules are selectable with their exact codes without enabling preview mode.
|
|
||||||
However, this behavior will display a warning and support will be removed in a future release.
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue