Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,53 @@
# dangling before dot
.b # trailing end-of-line
)

# Regression test for https://github.com/astral-sh/ruff/issues/19350
variable = (
(something) # a comment
.first_method("some string")
)

variable = (
something # a commentdddddddddddddddddddddddddddddd
.first_method("some string")
)

if (
(something) # a commentdddddddddddddddddddddddddddddd
.first_method("some string")
): pass

variable = (
(something # a comment
).first_method("some string")
)

if (
(something # a comment
).first_method("some string") # second comment
): pass

variable = ( # 1
# 2
(something) # 3
# 4
.first_method("some string") # 5
# 6
) # 7


if (
(something
# trailing own line on value
)
.first_method("some string")
): ...

variable = (
(something
# 1
) # 2
.first_method("some string")
)

17 changes: 16 additions & 1 deletion crates/ruff_python_formatter/src/expression/expr_attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,22 @@ impl NeedsParentheses for ExprAttribute {
context.comments().ranges(),
context.source(),
) {
OptionalParentheses::Never
// We have to avoid creating syntax errors like
// ```python
// variable = (something) # trailing
// .my_attribute
// ```
// See https://github.com/astral-sh/ruff/issues/19350
if context
.comments()
.trailing(self.value.as_ref())
.iter()
.any(|comment| comment.line_position().is_end_of_line())
{
OptionalParentheses::Multiline
} else {
OptionalParentheses::Never
}
} else {
self.value.needs_parentheses(self.into(), context)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,56 @@ result = (
# dangling before dot
.b # trailing end-of-line
)

# Regression test for https://github.com/astral-sh/ruff/issues/19350
variable = (
(something) # a comment
.first_method("some string")
)

variable = (
something # a commentdddddddddddddddddddddddddddddd
.first_method("some string")
)

if (
(something) # a commentdddddddddddddddddddddddddddddd
.first_method("some string")
): pass

variable = (
(something # a comment
).first_method("some string")
)

if (
(something # a comment
).first_method("some string") # second comment
): pass

variable = ( # 1
# 2
(something) # 3
# 4
.first_method("some string") # 5
# 6
) # 7


if (
(something
# trailing own line on value
)
.first_method("some string")
): ...

variable = (
(something
# 1
) # 2
.first_method("some string")
)

```

## Output
Expand Down Expand Up @@ -328,4 +378,54 @@ result = (
# dangling before dot
.b # trailing end-of-line
)

# Regression test for https://github.com/astral-sh/ruff/issues/19350
variable = (
(something) # a comment
.first_method("some string")
)

variable = something.first_method( # a commentdddddddddddddddddddddddddddddd
"some string"
)

if (
(something) # a commentdddddddddddddddddddddddddddddd
.first_method("some string")
):
pass

variable = (
something # a comment
).first_method("some string")

if (
(
something # a comment
).first_method("some string") # second comment
):
pass

variable = ( # 1
# 2
(something) # 3
# 4
.first_method("some string") # 5
# 6
) # 7


if (
something
# trailing own line on value
).first_method("some string"):
...

variable = (
(
something
# 1
) # 2
.first_method("some string")
)
```