mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-29 13:24:57 +00:00
[flake8-commas
] Stabilize support for trailing comma checks in type parameter lists (COM812
, COM819
) (#20275)
Introduced in #19390. Removed gating, updated tests. No documentation to update.
This commit is contained in:
parent
aef0a107a8
commit
ac7f882c78
6 changed files with 114 additions and 180 deletions
|
@ -222,11 +222,6 @@ pub(crate) const fn is_add_future_annotations_imports_enabled(settings: &LinterS
|
||||||
settings.preview.is_enabled()
|
settings.preview.is_enabled()
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://github.com/astral-sh/ruff/pull/19390
|
|
||||||
pub(crate) const fn is_trailing_comma_type_params_enabled(settings: &LinterSettings) -> bool {
|
|
||||||
settings.preview.is_enabled()
|
|
||||||
}
|
|
||||||
|
|
||||||
// https://github.com/astral-sh/ruff/pull/19851
|
// https://github.com/astral-sh/ruff/pull/19851
|
||||||
pub(crate) const fn is_maxsplit_without_separator_fix_enabled(settings: &LinterSettings) -> bool {
|
pub(crate) const fn is_maxsplit_without_separator_fix_enabled(settings: &LinterSettings) -> bool {
|
||||||
settings.preview.is_enabled()
|
settings.preview.is_enabled()
|
||||||
|
|
|
@ -10,7 +10,7 @@ mod tests {
|
||||||
|
|
||||||
use crate::registry::Rule;
|
use crate::registry::Rule;
|
||||||
use crate::test::test_path;
|
use crate::test::test_path;
|
||||||
use crate::{assert_diagnostics, assert_diagnostics_diff, settings};
|
use crate::{assert_diagnostics, settings};
|
||||||
|
|
||||||
#[test_case(Path::new("COM81.py"))]
|
#[test_case(Path::new("COM81.py"))]
|
||||||
#[test_case(Path::new("COM81_syntax_error.py"))]
|
#[test_case(Path::new("COM81_syntax_error.py"))]
|
||||||
|
@ -27,28 +27,4 @@ mod tests {
|
||||||
assert_diagnostics!(snapshot, diagnostics);
|
assert_diagnostics!(snapshot, diagnostics);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test_case(Path::new("COM81.py"))]
|
|
||||||
#[test_case(Path::new("COM81_syntax_error.py"))]
|
|
||||||
fn preview_rules(path: &Path) -> Result<()> {
|
|
||||||
let snapshot = format!("preview_diff__{}", path.to_string_lossy());
|
|
||||||
let rules = vec![
|
|
||||||
Rule::MissingTrailingComma,
|
|
||||||
Rule::TrailingCommaOnBareTuple,
|
|
||||||
Rule::ProhibitedTrailingComma,
|
|
||||||
];
|
|
||||||
let settings_before = settings::LinterSettings::for_rules(rules.clone());
|
|
||||||
let settings_after = settings::LinterSettings {
|
|
||||||
preview: crate::settings::types::PreviewMode::Enabled,
|
|
||||||
..settings::LinterSettings::for_rules(rules)
|
|
||||||
};
|
|
||||||
|
|
||||||
assert_diagnostics_diff!(
|
|
||||||
snapshot,
|
|
||||||
Path::new("flake8_commas").join(path).as_path(),
|
|
||||||
&settings_before,
|
|
||||||
&settings_after
|
|
||||||
);
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,8 +5,6 @@ use ruff_text_size::{Ranged, TextRange};
|
||||||
|
|
||||||
use crate::Locator;
|
use crate::Locator;
|
||||||
use crate::checkers::ast::LintContext;
|
use crate::checkers::ast::LintContext;
|
||||||
use crate::preview::is_trailing_comma_type_params_enabled;
|
|
||||||
use crate::settings::LinterSettings;
|
|
||||||
use crate::{AlwaysFixableViolation, Violation};
|
use crate::{AlwaysFixableViolation, Violation};
|
||||||
use crate::{Edit, Fix};
|
use crate::{Edit, Fix};
|
||||||
|
|
||||||
|
@ -298,7 +296,7 @@ pub(crate) fn trailing_commas(
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update the comma context stack.
|
// Update the comma context stack.
|
||||||
let context = update_context(token, prev, prev_prev, &mut stack, lint_context.settings());
|
let context = update_context(token, prev, prev_prev, &mut stack);
|
||||||
|
|
||||||
check_token(token, prev, prev_prev, context, locator, lint_context);
|
check_token(token, prev, prev_prev, context, locator, lint_context);
|
||||||
|
|
||||||
|
@ -417,7 +415,6 @@ fn update_context(
|
||||||
prev: SimpleToken,
|
prev: SimpleToken,
|
||||||
prev_prev: SimpleToken,
|
prev_prev: SimpleToken,
|
||||||
stack: &mut Vec<Context>,
|
stack: &mut Vec<Context>,
|
||||||
settings: &LinterSettings,
|
|
||||||
) -> Context {
|
) -> Context {
|
||||||
let new_context = match token.ty {
|
let new_context = match token.ty {
|
||||||
TokenType::OpeningBracket => match (prev.ty, prev_prev.ty) {
|
TokenType::OpeningBracket => match (prev.ty, prev_prev.ty) {
|
||||||
|
@ -427,19 +424,11 @@ fn update_context(
|
||||||
}
|
}
|
||||||
_ => Context::new(ContextType::Tuple),
|
_ => Context::new(ContextType::Tuple),
|
||||||
},
|
},
|
||||||
TokenType::OpeningSquareBracket if is_trailing_comma_type_params_enabled(settings) => {
|
TokenType::OpeningSquareBracket => match (prev.ty, prev_prev.ty) {
|
||||||
match (prev.ty, prev_prev.ty) {
|
(TokenType::Named, TokenType::Def | TokenType::Class | TokenType::Type) => {
|
||||||
(TokenType::Named, TokenType::Def | TokenType::Class | TokenType::Type) => {
|
Context::new(ContextType::TypeParameters)
|
||||||
Context::new(ContextType::TypeParameters)
|
|
||||||
}
|
|
||||||
(TokenType::ClosingBracket | TokenType::Named | TokenType::String, _) => {
|
|
||||||
Context::new(ContextType::Subscript)
|
|
||||||
}
|
|
||||||
_ => Context::new(ContextType::List),
|
|
||||||
}
|
}
|
||||||
}
|
(TokenType::ClosingBracket | TokenType::Named | TokenType::String, _) => {
|
||||||
TokenType::OpeningSquareBracket => match prev.ty {
|
|
||||||
TokenType::ClosingBracket | TokenType::Named | TokenType::String => {
|
|
||||||
Context::new(ContextType::Subscript)
|
Context::new(ContextType::Subscript)
|
||||||
}
|
}
|
||||||
_ => Context::new(ContextType::List),
|
_ => Context::new(ContextType::List),
|
||||||
|
|
|
@ -939,3 +939,111 @@ help: Add trailing comma
|
||||||
644 | )
|
644 | )
|
||||||
645 |
|
645 |
|
||||||
646 | assert False, f"<- This is not a trailing comma"
|
646 | assert False, f"<- This is not a trailing comma"
|
||||||
|
|
||||||
|
COM812 [*] Trailing comma missing
|
||||||
|
--> COM81.py:655:6
|
||||||
|
|
|
||||||
|
654 | type X[
|
||||||
|
655 | T
|
||||||
|
| ^
|
||||||
|
656 | ] = T
|
||||||
|
657 | def f[
|
||||||
|
|
|
||||||
|
help: Add trailing comma
|
||||||
|
652 | }"""
|
||||||
|
653 |
|
||||||
|
654 | type X[
|
||||||
|
- T
|
||||||
|
655 + T,
|
||||||
|
656 | ] = T
|
||||||
|
657 | def f[
|
||||||
|
658 | T
|
||||||
|
|
||||||
|
COM812 [*] Trailing comma missing
|
||||||
|
--> COM81.py:658:6
|
||||||
|
|
|
||||||
|
656 | ] = T
|
||||||
|
657 | def f[
|
||||||
|
658 | T
|
||||||
|
| ^
|
||||||
|
659 | ](): pass
|
||||||
|
660 | class C[
|
||||||
|
|
|
||||||
|
help: Add trailing comma
|
||||||
|
655 | T
|
||||||
|
656 | ] = T
|
||||||
|
657 | def f[
|
||||||
|
- T
|
||||||
|
658 + T,
|
||||||
|
659 | ](): pass
|
||||||
|
660 | class C[
|
||||||
|
661 | T
|
||||||
|
|
||||||
|
COM812 [*] Trailing comma missing
|
||||||
|
--> COM81.py:661:6
|
||||||
|
|
|
||||||
|
659 | ](): pass
|
||||||
|
660 | class C[
|
||||||
|
661 | T
|
||||||
|
| ^
|
||||||
|
662 | ]: pass
|
||||||
|
|
|
||||||
|
help: Add trailing comma
|
||||||
|
658 | T
|
||||||
|
659 | ](): pass
|
||||||
|
660 | class C[
|
||||||
|
- T
|
||||||
|
661 + T,
|
||||||
|
662 | ]: pass
|
||||||
|
663 |
|
||||||
|
664 | type X[T,] = T
|
||||||
|
|
||||||
|
COM819 [*] Trailing comma prohibited
|
||||||
|
--> COM81.py:664:9
|
||||||
|
|
|
||||||
|
662 | ]: pass
|
||||||
|
663 |
|
||||||
|
664 | type X[T,] = T
|
||||||
|
| ^
|
||||||
|
665 | def f[T,](): pass
|
||||||
|
666 | class C[T,]: pass
|
||||||
|
|
|
||||||
|
help: Remove trailing comma
|
||||||
|
661 | T
|
||||||
|
662 | ]: pass
|
||||||
|
663 |
|
||||||
|
- type X[T,] = T
|
||||||
|
664 + type X[T] = T
|
||||||
|
665 | def f[T,](): pass
|
||||||
|
666 | class C[T,]: pass
|
||||||
|
|
||||||
|
COM819 [*] Trailing comma prohibited
|
||||||
|
--> COM81.py:665:8
|
||||||
|
|
|
||||||
|
664 | type X[T,] = T
|
||||||
|
665 | def f[T,](): pass
|
||||||
|
| ^
|
||||||
|
666 | class C[T,]: pass
|
||||||
|
|
|
||||||
|
help: Remove trailing comma
|
||||||
|
662 | ]: pass
|
||||||
|
663 |
|
||||||
|
664 | type X[T,] = T
|
||||||
|
- def f[T,](): pass
|
||||||
|
665 + def f[T](): pass
|
||||||
|
666 | class C[T,]: pass
|
||||||
|
|
||||||
|
COM819 [*] Trailing comma prohibited
|
||||||
|
--> COM81.py:666:10
|
||||||
|
|
|
||||||
|
664 | type X[T,] = T
|
||||||
|
665 | def f[T,](): pass
|
||||||
|
666 | class C[T,]: pass
|
||||||
|
| ^
|
||||||
|
|
|
||||||
|
help: Remove trailing comma
|
||||||
|
663 |
|
||||||
|
664 | type X[T,] = T
|
||||||
|
665 | def f[T,](): pass
|
||||||
|
- class C[T,]: pass
|
||||||
|
666 + class C[T]: pass
|
||||||
|
|
|
@ -1,124 +0,0 @@
|
||||||
---
|
|
||||||
source: crates/ruff_linter/src/rules/flake8_commas/mod.rs
|
|
||||||
---
|
|
||||||
--- Linter settings ---
|
|
||||||
-linter.preview = disabled
|
|
||||||
+linter.preview = enabled
|
|
||||||
|
|
||||||
--- Summary ---
|
|
||||||
Removed: 0
|
|
||||||
Added: 6
|
|
||||||
|
|
||||||
--- Added ---
|
|
||||||
COM812 [*] Trailing comma missing
|
|
||||||
--> COM81.py:655:6
|
|
||||||
|
|
|
||||||
654 | type X[
|
|
||||||
655 | T
|
|
||||||
| ^
|
|
||||||
656 | ] = T
|
|
||||||
657 | def f[
|
|
||||||
|
|
|
||||||
help: Add trailing comma
|
|
||||||
652 | }"""
|
|
||||||
653 |
|
|
||||||
654 | type X[
|
|
||||||
- T
|
|
||||||
655 + T,
|
|
||||||
656 | ] = T
|
|
||||||
657 | def f[
|
|
||||||
658 | T
|
|
||||||
|
|
||||||
|
|
||||||
COM812 [*] Trailing comma missing
|
|
||||||
--> COM81.py:658:6
|
|
||||||
|
|
|
||||||
656 | ] = T
|
|
||||||
657 | def f[
|
|
||||||
658 | T
|
|
||||||
| ^
|
|
||||||
659 | ](): pass
|
|
||||||
660 | class C[
|
|
||||||
|
|
|
||||||
help: Add trailing comma
|
|
||||||
655 | T
|
|
||||||
656 | ] = T
|
|
||||||
657 | def f[
|
|
||||||
- T
|
|
||||||
658 + T,
|
|
||||||
659 | ](): pass
|
|
||||||
660 | class C[
|
|
||||||
661 | T
|
|
||||||
|
|
||||||
|
|
||||||
COM812 [*] Trailing comma missing
|
|
||||||
--> COM81.py:661:6
|
|
||||||
|
|
|
||||||
659 | ](): pass
|
|
||||||
660 | class C[
|
|
||||||
661 | T
|
|
||||||
| ^
|
|
||||||
662 | ]: pass
|
|
||||||
|
|
|
||||||
help: Add trailing comma
|
|
||||||
658 | T
|
|
||||||
659 | ](): pass
|
|
||||||
660 | class C[
|
|
||||||
- T
|
|
||||||
661 + T,
|
|
||||||
662 | ]: pass
|
|
||||||
663 |
|
|
||||||
664 | type X[T,] = T
|
|
||||||
|
|
||||||
|
|
||||||
COM819 [*] Trailing comma prohibited
|
|
||||||
--> COM81.py:664:9
|
|
||||||
|
|
|
||||||
662 | ]: pass
|
|
||||||
663 |
|
|
||||||
664 | type X[T,] = T
|
|
||||||
| ^
|
|
||||||
665 | def f[T,](): pass
|
|
||||||
666 | class C[T,]: pass
|
|
||||||
|
|
|
||||||
help: Remove trailing comma
|
|
||||||
661 | T
|
|
||||||
662 | ]: pass
|
|
||||||
663 |
|
|
||||||
- type X[T,] = T
|
|
||||||
664 + type X[T] = T
|
|
||||||
665 | def f[T,](): pass
|
|
||||||
666 | class C[T,]: pass
|
|
||||||
|
|
||||||
|
|
||||||
COM819 [*] Trailing comma prohibited
|
|
||||||
--> COM81.py:665:8
|
|
||||||
|
|
|
||||||
664 | type X[T,] = T
|
|
||||||
665 | def f[T,](): pass
|
|
||||||
| ^
|
|
||||||
666 | class C[T,]: pass
|
|
||||||
|
|
|
||||||
help: Remove trailing comma
|
|
||||||
662 | ]: pass
|
|
||||||
663 |
|
|
||||||
664 | type X[T,] = T
|
|
||||||
- def f[T,](): pass
|
|
||||||
665 + def f[T](): pass
|
|
||||||
666 | class C[T,]: pass
|
|
||||||
|
|
||||||
|
|
||||||
COM819 [*] Trailing comma prohibited
|
|
||||||
--> COM81.py:666:10
|
|
||||||
|
|
|
||||||
664 | type X[T,] = T
|
|
||||||
665 | def f[T,](): pass
|
|
||||||
666 | class C[T,]: pass
|
|
||||||
| ^
|
|
||||||
|
|
|
||||||
help: Remove trailing comma
|
|
||||||
663 |
|
|
||||||
664 | type X[T,] = T
|
|
||||||
665 | def f[T,](): pass
|
|
||||||
- class C[T,]: pass
|
|
||||||
666 + class C[T]: pass
|
|
|
@ -1,10 +0,0 @@
|
||||||
---
|
|
||||||
source: crates/ruff_linter/src/rules/flake8_commas/mod.rs
|
|
||||||
---
|
|
||||||
--- Linter settings ---
|
|
||||||
-linter.preview = disabled
|
|
||||||
+linter.preview = enabled
|
|
||||||
|
|
||||||
--- Summary ---
|
|
||||||
Removed: 0
|
|
||||||
Added: 0
|
|
Loading…
Add table
Add a link
Reference in a new issue