Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Formatter: Trailing comment on list gets formatted to a separate line #6367

Closed
dhruvmanila opened this issue Aug 5, 2023 · 3 comments · Fixed by #6380
Closed

Formatter: Trailing comment on list gets formatted to a separate line #6367

dhruvmanila opened this issue Aug 5, 2023 · 3 comments · Fixed by #6380
Assignees
Labels
formatter Related to the formatter

Comments

@dhruvmanila
Copy link
Member

dhruvmanila commented Aug 5, 2023

Given:

[  # comment
    first,
    second,
    third
]  # another comment

Notice that there's no magic trailing comma, so the list will be collapsed.

Ruff:

[first, second, third]  # comment
  # another comment

Black:

[first, second, third]  # comment  # another comment
@dhruvmanila dhruvmanila added the formatter Related to the formatter label Aug 5, 2023
@charliermarsh
Copy link
Member

I can take a look.

@charliermarsh charliermarsh self-assigned this Aug 5, 2023
@dhruvmanila
Copy link
Member Author

dhruvmanila commented Aug 5, 2023

This might be relevant: I noticed it first while working on the StmtMatch node and one of the test cases contained a list as the match subject:

match [ # comment
first,
second,
third
]: # another comment

which gets formatted correctly (although the comments are in wrong order) when using dangling_comments:

match [first, second, third]: # another comment # comment

But, it gets formatted in the way mentioned in the issue description when using trailing_comments here:

dangling_comments(dangling_item_comments)

@charliermarsh
Copy link
Member

It kind of feels like a bug in line_suffix, but I need to understand the implementation better to say for certain.

charliermarsh added a commit that referenced this issue Aug 7, 2023
## Summary

Given:

```python
[  # comment
    first,
    second,
    third
]  # another comment
```

We were adding a hard line break as part of the formatting of `#
comment`, which led to the following formatting:

```python
[first, second, third]  # comment
  # another comment
```

Closes #6367.
durumu pushed a commit to durumu/ruff that referenced this issue Aug 12, 2023
…l-sh#6380)

## Summary

Given:

```python
[  # comment
    first,
    second,
    third
]  # another comment
```

We were adding a hard line break as part of the formatting of `#
comment`, which led to the following formatting:

```python
[first, second, third]  # comment
  # another comment
```

Closes astral-sh#6367.
@MichaReiser MichaReiser added this to the Formatter: Alpha milestone Aug 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
formatter Related to the formatter
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants