Skip to content

Commit ac7f882

Browse files
dylwil3ntBre
authored andcommitted
[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.
1 parent aef0a10 commit ac7f882

6 files changed

+114
-180
lines changed

crates/ruff_linter/src/preview.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -222,11 +222,6 @@ pub(crate) const fn is_add_future_annotations_imports_enabled(settings: &LinterS
222222
settings.preview.is_enabled()
223223
}
224224

225-
// https://github.com/astral-sh/ruff/pull/19390
226-
pub(crate) const fn is_trailing_comma_type_params_enabled(settings: &LinterSettings) -> bool {
227-
settings.preview.is_enabled()
228-
}
229-
230225
// https://github.com/astral-sh/ruff/pull/19851
231226
pub(crate) const fn is_maxsplit_without_separator_fix_enabled(settings: &LinterSettings) -> bool {
232227
settings.preview.is_enabled()

crates/ruff_linter/src/rules/flake8_commas/mod.rs

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ mod tests {
1010

1111
use crate::registry::Rule;
1212
use crate::test::test_path;
13-
use crate::{assert_diagnostics, assert_diagnostics_diff, settings};
13+
use crate::{assert_diagnostics, settings};
1414

1515
#[test_case(Path::new("COM81.py"))]
1616
#[test_case(Path::new("COM81_syntax_error.py"))]
@@ -27,28 +27,4 @@ mod tests {
2727
assert_diagnostics!(snapshot, diagnostics);
2828
Ok(())
2929
}
30-
31-
#[test_case(Path::new("COM81.py"))]
32-
#[test_case(Path::new("COM81_syntax_error.py"))]
33-
fn preview_rules(path: &Path) -> Result<()> {
34-
let snapshot = format!("preview_diff__{}", path.to_string_lossy());
35-
let rules = vec![
36-
Rule::MissingTrailingComma,
37-
Rule::TrailingCommaOnBareTuple,
38-
Rule::ProhibitedTrailingComma,
39-
];
40-
let settings_before = settings::LinterSettings::for_rules(rules.clone());
41-
let settings_after = settings::LinterSettings {
42-
preview: crate::settings::types::PreviewMode::Enabled,
43-
..settings::LinterSettings::for_rules(rules)
44-
};
45-
46-
assert_diagnostics_diff!(
47-
snapshot,
48-
Path::new("flake8_commas").join(path).as_path(),
49-
&settings_before,
50-
&settings_after
51-
);
52-
Ok(())
53-
}
5430
}

crates/ruff_linter/src/rules/flake8_commas/rules/trailing_commas.rs

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ use ruff_text_size::{Ranged, TextRange};
55

66
use crate::Locator;
77
use crate::checkers::ast::LintContext;
8-
use crate::preview::is_trailing_comma_type_params_enabled;
9-
use crate::settings::LinterSettings;
108
use crate::{AlwaysFixableViolation, Violation};
119
use crate::{Edit, Fix};
1210

@@ -298,7 +296,7 @@ pub(crate) fn trailing_commas(
298296
}
299297

300298
// Update the comma context stack.
301-
let context = update_context(token, prev, prev_prev, &mut stack, lint_context.settings());
299+
let context = update_context(token, prev, prev_prev, &mut stack);
302300

303301
check_token(token, prev, prev_prev, context, locator, lint_context);
304302

@@ -417,7 +415,6 @@ fn update_context(
417415
prev: SimpleToken,
418416
prev_prev: SimpleToken,
419417
stack: &mut Vec<Context>,
420-
settings: &LinterSettings,
421418
) -> Context {
422419
let new_context = match token.ty {
423420
TokenType::OpeningBracket => match (prev.ty, prev_prev.ty) {
@@ -427,19 +424,11 @@ fn update_context(
427424
}
428425
_ => Context::new(ContextType::Tuple),
429426
},
430-
TokenType::OpeningSquareBracket if is_trailing_comma_type_params_enabled(settings) => {
431-
match (prev.ty, prev_prev.ty) {
432-
(TokenType::Named, TokenType::Def | TokenType::Class | TokenType::Type) => {
433-
Context::new(ContextType::TypeParameters)
434-
}
435-
(TokenType::ClosingBracket | TokenType::Named | TokenType::String, _) => {
436-
Context::new(ContextType::Subscript)
437-
}
438-
_ => Context::new(ContextType::List),
427+
TokenType::OpeningSquareBracket => match (prev.ty, prev_prev.ty) {
428+
(TokenType::Named, TokenType::Def | TokenType::Class | TokenType::Type) => {
429+
Context::new(ContextType::TypeParameters)
439430
}
440-
}
441-
TokenType::OpeningSquareBracket => match prev.ty {
442-
TokenType::ClosingBracket | TokenType::Named | TokenType::String => {
431+
(TokenType::ClosingBracket | TokenType::Named | TokenType::String, _) => {
443432
Context::new(ContextType::Subscript)
444433
}
445434
_ => Context::new(ContextType::List),

crates/ruff_linter/src/rules/flake8_commas/snapshots/ruff_linter__rules__flake8_commas__tests__COM81.py.snap

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -939,3 +939,111 @@ help: Add trailing comma
939939
644 | )
940940
645 |
941941
646 | assert False, f"<- This is not a trailing comma"
942+
943+
COM812 [*] Trailing comma missing
944+
--> COM81.py:655:6
945+
|
946+
654 | type X[
947+
655 | T
948+
| ^
949+
656 | ] = T
950+
657 | def f[
951+
|
952+
help: Add trailing comma
953+
652 | }"""
954+
653 |
955+
654 | type X[
956+
- T
957+
655 + T,
958+
656 | ] = T
959+
657 | def f[
960+
658 | T
961+
962+
COM812 [*] Trailing comma missing
963+
--> COM81.py:658:6
964+
|
965+
656 | ] = T
966+
657 | def f[
967+
658 | T
968+
| ^
969+
659 | ](): pass
970+
660 | class C[
971+
|
972+
help: Add trailing comma
973+
655 | T
974+
656 | ] = T
975+
657 | def f[
976+
- T
977+
658 + T,
978+
659 | ](): pass
979+
660 | class C[
980+
661 | T
981+
982+
COM812 [*] Trailing comma missing
983+
--> COM81.py:661:6
984+
|
985+
659 | ](): pass
986+
660 | class C[
987+
661 | T
988+
| ^
989+
662 | ]: pass
990+
|
991+
help: Add trailing comma
992+
658 | T
993+
659 | ](): pass
994+
660 | class C[
995+
- T
996+
661 + T,
997+
662 | ]: pass
998+
663 |
999+
664 | type X[T,] = T
1000+
1001+
COM819 [*] Trailing comma prohibited
1002+
--> COM81.py:664:9
1003+
|
1004+
662 | ]: pass
1005+
663 |
1006+
664 | type X[T,] = T
1007+
| ^
1008+
665 | def f[T,](): pass
1009+
666 | class C[T,]: pass
1010+
|
1011+
help: Remove trailing comma
1012+
661 | T
1013+
662 | ]: pass
1014+
663 |
1015+
- type X[T,] = T
1016+
664 + type X[T] = T
1017+
665 | def f[T,](): pass
1018+
666 | class C[T,]: pass
1019+
1020+
COM819 [*] Trailing comma prohibited
1021+
--> COM81.py:665:8
1022+
|
1023+
664 | type X[T,] = T
1024+
665 | def f[T,](): pass
1025+
| ^
1026+
666 | class C[T,]: pass
1027+
|
1028+
help: Remove trailing comma
1029+
662 | ]: pass
1030+
663 |
1031+
664 | type X[T,] = T
1032+
- def f[T,](): pass
1033+
665 + def f[T](): pass
1034+
666 | class C[T,]: pass
1035+
1036+
COM819 [*] Trailing comma prohibited
1037+
--> COM81.py:666:10
1038+
|
1039+
664 | type X[T,] = T
1040+
665 | def f[T,](): pass
1041+
666 | class C[T,]: pass
1042+
| ^
1043+
|
1044+
help: Remove trailing comma
1045+
663 |
1046+
664 | type X[T,] = T
1047+
665 | def f[T,](): pass
1048+
- class C[T,]: pass
1049+
666 + class C[T]: pass

crates/ruff_linter/src/rules/flake8_commas/snapshots/ruff_linter__rules__flake8_commas__tests__preview_diff__COM81.py.snap

Lines changed: 0 additions & 124 deletions
This file was deleted.

crates/ruff_linter/src/rules/flake8_commas/snapshots/ruff_linter__rules__flake8_commas__tests__preview_diff__COM81_syntax_error.py.snap

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)