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

request: add trailing commas #1058

Closed
MartinBernstorff opened this issue Dec 5, 2022 · 3 comments · Fixed by #1872
Closed

request: add trailing commas #1058

MartinBernstorff opened this issue Dec 5, 2022 · 3 comments · Fixed by #1872
Labels
plugin Implementing a known but unsupported plugin

Comments

@MartinBernstorff
Copy link

Only thing in my pre-commit toolchain that Ruff hasn't replaced yet. Would love to see it, and thanks for all your work!

https://github.com/asottile/add-trailing-comma

@charliermarsh charliermarsh added enhancement plugin Implementing a known but unsupported plugin and removed enhancement labels Dec 30, 2022
@bluetech
Copy link
Contributor

There's also flake8-commas. It has these rules:

Code message
C812 missing trailing comma
C813 missing trailing comma in Python 3
C814 missing trailing comma in Python 2
C815 missing trailing comma in Python 3.5+
C816 missing trailing comma in Python 3.6+
C818 trailing comma on bare tuple prohibited
C819 trailing comma prohibited

charliermarsh pushed a commit that referenced this issue Jan 15, 2023
Implements [flake8-commas](https://github.com/PyCQA/flake8-commas). Fixes #1058.

The plugin is mostly redundant with Black (and also deprecated upstream), but very useful for projects which can't/won't use an auto-formatter. 

This linter works on tokens. Before porting to Rust, I cleaned up the Python code ([link](https://gist.github.com/bluetech/7c5dcbdec4a73dd5a74d4bc09c72b8b9)) and made sure the tests pass. In the Rust version I tried to add explanatory comments, to the best of my understanding of the original logic.

Some changes I did make:

- Got rid of rule C814 - "missing trailing comma in Python 2". Ruff doesn't support Python 2.
- Merged rules C815 - "missing trailing comma in Python 3.5+" and C816 - "missing trailing comma in Python 3.6+" into C812 - "missing trailing comma". These Python versions are outdated, didn't think it was worth the complication.
- Added autofixes for C812 and C819.

Autofix is missing for C818 - "trailing comma on bare tuple prohibited". It needs to turn e.g. `x = 1,` into `x = (1, )`, it's a bit difficult to do with tokens only, so I skipped it for now.

I ran the rules on cpython/Lib and on a big internal code base and it works as intended (though I only sampled the diffs).
@Avasam
Copy link

Avasam commented Mar 15, 2023

add-trailing-comma also has unhug-trailing-paren, unhug-leading-paren and avoids arbitrary indentation.
Which is not enforced by Ruff or Autopep8. Even Black only enforces it when magic commas are used:

# This is found acceptable by Ruff, Autopep8 and Black
print(1,
      2,
      3,
      4,
      5)
      
# This is what add-trailing-commas changes it to
print(
    1,
    2,
    3,
    4,
    5,
)

So Ruff hasn't yet completely replaced it for me, though I'd love to see it get there. Is this the right place or should I open a new issue?

@sisrfeng
Copy link

sisrfeng commented Aug 17, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
plugin Implementing a known but unsupported plugin
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants