diff --git a/crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/attribute.py b/crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/attribute.py index e6cb4b0ac5a01..63fb3954dcd20 100644 --- a/crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/attribute.py +++ b/crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/attribute.py @@ -106,4 +106,6 @@ a.b ) - +# regression: https://github.com/astral-sh/ruff/issues/6181 +(# +()).a diff --git a/crates/ruff_python_formatter/src/comments/placement.rs b/crates/ruff_python_formatter/src/comments/placement.rs index 2c77b020318e1..a2e66ec6fced9 100644 --- a/crates/ruff_python_formatter/src/comments/placement.rs +++ b/crates/ruff_python_formatter/src/comments/placement.rs @@ -998,10 +998,14 @@ fn handle_attribute_comment<'a>( comment: DecoratedComment<'a>, attribute: &'a ast::ExprAttribute, ) -> CommentPlacement<'a> { - debug_assert!( - comment.preceding_node().is_some(), - "The enclosing node an attribute expression, expected to be at least after the name" - ); + if comment.preceding_node().is_none() { + // ```text + // ( value) . attr + // ^^^^ we're in this range + // ``` + + return CommentPlacement::leading(attribute.value.as_ref(), comment); + } // ```text // value . attr diff --git a/crates/ruff_python_formatter/tests/snapshots/format@expression__attribute.py.snap b/crates/ruff_python_formatter/tests/snapshots/format@expression__attribute.py.snap index f1a5dbb035c50..00dccd7c7ddae 100644 --- a/crates/ruff_python_formatter/tests/snapshots/format@expression__attribute.py.snap +++ b/crates/ruff_python_formatter/tests/snapshots/format@expression__attribute.py.snap @@ -112,7 +112,9 @@ x6 = ( a.b ) - +# regression: https://github.com/astral-sh/ruff/issues/6181 +(# +()).a ``` ## Output @@ -200,6 +202,12 @@ x6 = ( # Check assumption with enclosing nodes a.b ) + +# regression: https://github.com/astral-sh/ruff/issues/6181 +( + # + () +).a ```