-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
Black support for the Python codebase #4297
Conversation
OMG!!! 🏴 |
This PR adds a check to CircleCI that will fail if the code is not formatted with Black. Considering that formatting with Black (and Prettier) can be automated I wonder what we should do:
Any thoughts? |
I would suggest to go with (1) and add a pre-commit config. Since the CI job is already separate, it's easy to see why a test failed from the GitHub status UI. But maybe rename the job to "black-formatting" as it's more obvious to contributors than just "black"? |
It's relatively easy to understand what job failed. But I'm not sure it's obvious to everyone (specially new people) what this failure means, how to fix it, etc. Another aspect that it's boring to do things a machine can do :-) Maybe we can start by adding a comment on the PR if it fails that explains what Black is and how to remedy? |
I've been experimenting with a middle ground solution: ability to trigger GitHub Action to run Black & Prettier with a comment on the PR. The end solution I was aiming for is that the check will post a comment on the PR with a text like:
Then the 👍 reaction would trigger the GitHub Action. My POC used a comment "slash command", but apparently comments run with the context of the master branch. To trigger this on the correct branch/repo there is more work required. I'm posting here my work just in case someone might want to pick it up:
The idea is to infer the Pull Request from the GitHub event data (like this action) and then use the Pull Request number to get the branch/repo info using GitHub's API. |
Another option: run the auto formatter when merging into master? |
I changed the implementation here a bit: instead of checking whether the code is "compliant" with Black's formatting, I added a GitHub Action to format any code committed to master. Considering this is an automatic step (and hopefully non destructive) it felt counter productive to block Pull Requests based on whether they are formatted with Black or not. I will apply the same logic to Prettier integration in another Pull Request. |
Interesting idea! Could I interest you in also auto applying isort? There is a black-compatible config here: https://black.readthedocs.io/en/stable/the_black_code_style.html#how-black-wraps-lines |
The problem with applying isort is that unlike Black it might change actual implementation in case some imports have side effects and their order is important. While side effects on module import is a bad idea, it does happen. I don't remember if the main codebase has them (I hope not, but...) but I know for sure that we have some at the moment in the SaaS code base to deal with Python 2/3 Pickle compatibility and some other things related to running Celery/RQ and Python 2/3 codebases in a single environment. So while I'm not opposed to |
@arikfr Makes total sense, we've seen import time side effects as well, when we worked on our extensions. Maybe instead we could apply isort carefully once and exclude the delicate import statements with explicit skip comments? Then we could at least run an isort check on pull requests via a GH action? |
Ok, the idea to apply on merge to master is a bust:
|
Ah, d'oh! 🤦🏻♂️ |
@arikfr IIRC you can restrict who can push to master, maybe since you're using an API token for this, it can be applied to only being you? |
It won't help, because the required status check protection will fail the push. Also, I don't think we control with whose token it runs. The two options I thought of: Two options now:
|
In my experience new contributors often struggle with understanding the usefulness of code formatters and can be quite frustrated by checks failing because of code formatting. So anything automated or easy to run black would make sense the most. E.g. running a single command locally to apply the code formatting. For practiced developers this may not be a huge barrier. There is still the black_out bot, that we could run ourselves: https://github.com/Mariatta/black_out OTOH since I've seen many projects adopting it, maybe using pre-commit for black and prettier would make sense? There are a few people investigating ways to use it for bots as well: pre-commit/pre-commit.com#211 E.g. https://restyled.io/? |
@jezdez thanks! I will try restyled.io. I think it will do what we need with minimum effort for all involved. |
Restyled: 0a2dd83. |
@arikfr Huzzah! |
What type of PR is this? (check all applicable)
Description
Related Tickets & Documents
Closes #3806.