diff --git a/crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/type_alias.py b/crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/type_alias.py index c50a35a044..8e29cd3730 100644 --- a/crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/type_alias.py +++ b/crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/type_alias.py @@ -81,6 +81,10 @@ type type_params_comments[ # trailing open bracket comment D, # trailing comment # leading close bracket comment ] = int # trailing value comment +type type_params_single_comment[ # trailing open bracket comment + A, + B +] = int type type_params_all_kinds[type_var, *type_var_tuple, **param_spec] = int # type variable bounds diff --git a/crates/ruff_python_formatter/src/type_param/type_params.rs b/crates/ruff_python_formatter/src/type_param/type_params.rs index c99020cc24..8e40495e4a 100644 --- a/crates/ruff_python_formatter/src/type_param/type_params.rs +++ b/crates/ruff_python_formatter/src/type_param/type_params.rs @@ -1,11 +1,10 @@ -use ruff_formatter::write; use ruff_formatter::FormatResult; use ruff_python_ast::AstNode; use ruff_python_ast::TypeParams; use ruff_text_size::Ranged; use crate::builders::PyFormatterExtensions; -use crate::comments::{trailing_comments, SourceComment}; +use crate::comments::SourceComment; use crate::expression::parentheses::parenthesized; use crate::prelude::*; @@ -24,7 +23,6 @@ impl FormatNodeRule for FormatTypeParams { // ] = ... let comments = f.context().comments().clone(); let dangling_comments = comments.dangling(item.as_any_node_ref()); - write!(f, [trailing_comments(dangling_comments)])?; let items = format_with(|f| { f.join_comma_separated(item.end()) @@ -32,7 +30,9 @@ impl FormatNodeRule for FormatTypeParams { .finish() }); - parenthesized("[", &items, "]").fmt(f) + parenthesized("[", &items, "]") + .with_dangling_comments(dangling_comments) + .fmt(f) } fn fmt_dangling_comments( diff --git a/crates/ruff_python_formatter/tests/snapshots/format@parentheses__opening_parentheses_comment_value.py.snap b/crates/ruff_python_formatter/tests/snapshots/format@parentheses__opening_parentheses_comment_value.py.snap index 0c287d1b0b..5e387b26fa 100644 --- a/crates/ruff_python_formatter/tests/snapshots/format@parentheses__opening_parentheses_comment_value.py.snap +++ b/crates/ruff_python_formatter/tests/snapshots/format@parentheses__opening_parentheses_comment_value.py.snap @@ -327,7 +327,9 @@ f3 = { # f3 # Non-empty parentheses: These are not allowed without a value -def f1[T](): # f1 +def f1[ # f1 + T +](): pass diff --git a/crates/ruff_python_formatter/tests/snapshots/format@statement__type_alias.py.snap b/crates/ruff_python_formatter/tests/snapshots/format@statement__type_alias.py.snap index 1009367aa6..24df936b5a 100644 --- a/crates/ruff_python_formatter/tests/snapshots/format@statement__type_alias.py.snap +++ b/crates/ruff_python_formatter/tests/snapshots/format@statement__type_alias.py.snap @@ -87,6 +87,10 @@ type type_params_comments[ # trailing open bracket comment D, # trailing comment # leading close bracket comment ] = int # trailing value comment +type type_params_single_comment[ # trailing open bracket comment + A, + B +] = int type type_params_all_kinds[type_var, *type_var_tuple, **param_spec] = int # type variable bounds @@ -201,6 +205,10 @@ type type_params_comments[ # trailing open bracket comment D, # trailing comment # leading close bracket comment ] = int # trailing value comment +type type_params_single_comment[ # trailing open bracket comment + A, + B, +] = int type type_params_all_kinds[type_var, *type_var_tuple, **param_spec] = int # type variable bounds