Associate a trailing end-of-line comment in a parenthesized implicit concatenated string with the last literal (#15378)

This commit is contained in:
Micha Reiser 2025-01-10 19:21:34 +01:00 committed by GitHub
parent adca7bd95c
commit 2b28d566a4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 378 additions and 8 deletions

View file

@ -1,7 +1,6 @@
---
source: crates/ruff_python_formatter/tests/fixtures.rs
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/fstring.py
snapshot_kind: text
---
## Input
```python

View file

@ -297,7 +297,90 @@ aaaaa[aaaaaaaaaaa] = (
f"testeeeeeeeeeeeeeeeeeeeeeeeee{a
=}" "moreeeeeeeeeeeeeeeeeetest" # comment
)
```
# Trailing last-part comments
a = (
"a"
"b" # belongs to `b`
)
a: Literal[str] = (
"a"
"b" # belongs to `b`
)
a += (
"a"
"b" # belongs to `b`
)
a = (
r"a"
r"b" # belongs to `b`
)
a = (
"a"
"b"
) # belongs to the assignment
a = (((
"a"
"b" # belongs to `b`
)))
a = (((
"a"
"b"
) # belongs to the f-string expression
))
a = (
"a" "b" # belongs to the f-string expression
)
a = (
"a" "b"
# belongs to the f-string expression
)
# There's no "right" answer if some parts are on the same line while others are on separate lines.
# This is likely a comment for one of the last two parts but could also just be a comment for the entire f-string expression.
# Because there's no right answer, follow what we do elsewhere and associate the comment with the outer-most node which
# is the f-string expression.
a = (
"a"
"b" "ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc" # belongs to the f-string expression
)
logger.error(
f"Failed to run task {task} for job"
f"with id {str(job.id)}" # type: ignore[union-attr]
)
a = (10 +
"Exception in {call_back_name} "
f"'{msg}'" # belongs to binary operation
)
a = 10 + (
"Exception in {call_back_name} "
f"'{msg}'" # belongs to f-string
)
self._attr_unique_id = (
f"{self._device.temperature.group_address_state}_"
f"{self._device.target_temperature.group_address_state}_"
f"{self._device.target_temperature.group_address}_"
f"{self._device._setpoint_shift.group_address}" # noqa: SLF001
)
return (
f"Exception in {call_back_name} when handling msg on "
f"'{msg.topic}': '{msg.payload}'" # type: ignore[str-bytes-safe]
)```
## Output
```python
@ -619,4 +702,77 @@ aaaaa[aaaaaaaaaaa] = (
=}"
"moreeeeeeeeeeeeeeeeeetest" # comment
)
# Trailing last-part comments
a = (
"a"
"b" # belongs to `b`
)
a: Literal[str] = (
"a"
"b" # belongs to `b`
)
a += (
"a"
"b" # belongs to `b`
)
a = (
r"a"
r"b" # belongs to `b`
)
a = "ab" # belongs to the assignment
a = (
"a"
"b" # belongs to `b`
)
a = "ab" # belongs to the f-string expression
a = "ab" # belongs to the f-string expression
a = (
"ab"
# belongs to the f-string expression
)
# There's no "right" answer if some parts are on the same line while others are on separate lines.
# This is likely a comment for one of the last two parts but could also just be a comment for the entire f-string expression.
# Because there's no right answer, follow what we do elsewhere and associate the comment with the outer-most node which
# is the f-string expression.
a = (
"a"
"b"
"ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc"
) # belongs to the f-string expression
logger.error(
f"Failed to run task {task} for jobwith id {str(job.id)}" # type: ignore[union-attr]
)
a = (
10 + f"Exception in {{call_back_name}} '{msg}'" # belongs to binary operation
)
a = 10 + (
f"Exception in {{call_back_name}} '{msg}'" # belongs to f-string
)
self._attr_unique_id = (
f"{self._device.temperature.group_address_state}_"
f"{self._device.target_temperature.group_address_state}_"
f"{self._device.target_temperature.group_address}_"
f"{self._device._setpoint_shift.group_address}" # noqa: SLF001
)
return (
f"Exception in {call_back_name} when handling msg on "
f"'{msg.topic}': '{msg.payload}'" # type: ignore[str-bytes-safe]
)
```