-
Notifications
You must be signed in to change notification settings - Fork 15
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
Format all python code with black #78
Conversation
I am aware of black and also experimented with it a bit. In general, I am in favor of a consistent code format. Could you look into how to make it a pre-commit hook, which only reformats the changed lines of a commit? That way we can make sure that any new code is formatted correctly, without obfuscating the true changes a commit made, because everything was reformatted. |
I already looked into that a bit, that was the reason for posting the link to the discussion on partial formatting. I should've been a bit more explicit there. The gist is that the black authors made an explicit design decision not to support partial formatting, i.e. only formatting / checking the lines that were changed. There are third party projects / hacks to support this anyway, but they're not perfect. There's darken which (ab-)uses Then there is black-macciato, which apparently just cuts out the relevant lines and passes them through black individually. Not sure how well that works, but it doesn't sound very robust. A third option would be to gradually format some files and mark them with a specific keyword in a comment. Formatting would then only be checked for those files. |
That’s indeed inconvenient. I would be fine with solutions like darken or black-macciato, if the tools are reliable and if it is foreseeable that they will be maintained in the future. The third option does sound like more of a hassle than simply nit-picking for every pull request, that the code format should be followed. The fourth option of course being doing a complete reformat, with all the consequences (e.g. broken blame, future reformatting because of changes in black etc). |
Its hard to judge if they will remain maintained. They both have very little activity. But given that they are pretty simple tools, maybe there just isn't much maintenance needed.
The third option could also be automated, but I agree that it sort of just combines the "worst of both worlds" since it will still require a separate reformat commit per file.
I would prefer that option while it's still feasible.
A significant downside of the incremental approaches is that they can be inconvenient for the user. It would require them to use our pre-commit hook or figure out how to apply partial formatting themselves. If we just format everything, they can choose whether to use the pre-commit hook or juts manually invoke black. Of course doing nothing is also a valid option. Should I look further into the incremental options (darken an macciato)? |
Good points, after thinking about this a bit more and checking out different projects using pre-commit hooks, I am also in favor of this option. What I think needs to be done:
|
I'll add CI & docs in follow-up PRs. |
Description
This PR formats all python code with black, the "uncompromising Python code formatter".
Motivation and Context
They state the benefits in their README:
However there are downsides as well. I can think of two:
Personally, I do think that the benefits (uniform formatting, no nit-picking in reviews, not having to think about formatting when writing code) far outweigh the downsides. Especially at the early stage we are now, the downsides are comparatively low. There are not a lot of PRs to rebase and much of the code is intended to be improved in the future anyways.
Still, this PR is intended a basis of discussion first of all. What do you think?
A few relevant links:
If we were to do this, we'd also configure travis to check the formatting and provide a pre-commit specification to automatically apply it.
How Has This Been Tested?
Black makes sure that the AST before formatting is equal to the AST after formatting.
Does this close/impact existing issues?
N/A
Types of changes
Checklist: