Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork3.2k
[Developer Experience] Implement black as code formatter#7125
-
To ensure the code looks consistent across all the files and remove all style related arguments from the code, I propose that we move to black as our official code formatter. Once decided, we also release instructions on how to configure it and even an example to add it as a pre-commit hook. A lot of python projects and libraries have moved to black for formatting, and I think we should join the wagon too. I am curious to know what everyone thinks about it? |
BetaWas this translation helpful?Give feedback.
All reactions
Replies: 8 comments 10 replies
-
I'm yet to ever use black. I do see it used a lot though in OSS. Typically at work we've only ever been in fairly small teams so it's easy to get everyone to follow the same kind of structure. I can see the benefit of it on projects like this though. Even if it might completely destroy my preferred coding styles 😆 We could also put black and flake8 into pre-commit hooks so that they're being ran as people work rather than just when things hit github actions. |
BetaWas this translation helpful?Give feedback.
All reactions
-
For anyone wanting to explore what this would provide, for django-cms or any other project, I've been looking into hooks lately. Starting with...https://pre-commit.com/ There's a python package you can install, Then create a file in the root of your project called To simply run black populate that file with
However you could run isort, flake8, black and various other helpful tools to keep the code clean & tidy. I'm currently investigating this pre-commit setup, which as of yet, doesn't run black, but does run other useful tools;
|
BetaWas this translation helpful?Give feedback.
All reactions
-
pre-commit is a great tool and I have seen many people using it. |
BetaWas this translation helpful?Give feedback.
All reactions
-
Just adding a vote for black. I fought tooth and nail against it for a long while, trying to keep my own habits in place. After finally succumbing a couple of years ago, I have to say it's been amazing, both for my own projects and for collaborative work. The sheer number of open source projects basing around it make PRs so much easier once i caved. |
BetaWas this translation helpful?Give feedback.
All reactions
👍 1
-
My biggest issue with installing code formatters on existing projects is that the entire codebase is changed in one commit and tools such as Blame become useless to track changes and help bugfix. This will also make porting changes to v4 more difficult so of course I will complain. :-) Haha. I see a place for tools like this on new projects. If there was a way to force new changes to respect the rules that would help as it is implemented on each change and won't affect the code change history. |
BetaWas this translation helpful?Give feedback.
All reactions
-
That's a great point. Could do with introducing it somewhere without a run on all files to see how it works. |
BetaWas this translation helpful?Give feedback.
All reactions
-
Alright, I've just tried to take a look at black and understand how you can make it work for a project... but you can't. It seems you cannot configure it beyond some basics like line length. Giving it a run on django-bleach to see how it's code looks and I've gone off it already. It's ruined the consistency of my code. I'd rather we didn't use this package and instead just had flake8 running how we want as that will keep code formatted well, while allowing a little freedom. |
BetaWas this translation helpful?Give feedback.
All reactions
-
I may have been a little too quick to judge. It seems there are ways to work around this type of reformatting.
|
BetaWas this translation helpful?Give feedback.
All reactions
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
As I haven’t contributed to the django CMS code so far, I don’t feel in the position to ask for any direction to go in this matter, I can only share my experience: Until last year I just pressed sometimes Ctrl-Alt-L on PyCharm in order to do some automatic formatting, then I read a comparison of autopep8, black and yapf. It seemed to be a fair comparision. If I can find it again, I’ll let you know. autopep8 seemed to be not so good, black and yapf with Facebook style yielded similar results. Some people like it that black is as good as not configurable, because you don’t have to think about it, and you avoid discussions about the configuration in a team. yapf is slower than black, but you can configure many parameters, that’s the main reason why I’ve chosen it for my code, and so far I’m satisfied with the result. Also for yapf there are markers that tell it not to touch code fragments that it would mess up otherwise. Btw: Unlike me the author of the comparison chose black, but also said that the other formatters were good. yapf comes with four standard configs: yapf, pep8, Google, Facebook. It’s a project by Google, and apparently also used by Facebook. So if your concern of using black is that it’s hardly configurable, you might also want to give yapf a try. |
BetaWas this translation helpful?Give feedback.
All reactions
👍 2
-
@marksweb made an interesting point today in the TC meeting:
|
BetaWas this translation helpful?Give feedback.
All reactions
👍 1
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
I really like the idea of running eitherblack orblue. The drawback of having it kinda of messing with the git history is really a pain, but I think that we can overcome this problem by setting up it to be run in someparts of the code, like piece by piece instead of the whole project at once. This way we can iterate over it in a better way. There's also someways where we can integrate it in our CI process (however it will need some more work to be done. Just would love to hear what other folks think about it. |
BetaWas this translation helpful?Give feedback.
All reactions
-
We discussed this last year and he consensus was that we don't like the way black works. We decided that with a pre-commit config including flake8, isort & a few other hooks we can maintain quality code. For a specific example, in my screenshot above, running black actually makes the code less readable. Model field attributes go from. being per-line to one long line. I find it incredibly easy to read models where the attributes are split by line, whereas scrolling through 1 line takes much longer. If you've been reading through the project and had issue with the format, can you cite any examples? |
BetaWas this translation helpful?Give feedback.
All reactions
-
As mentioned in our Friday meetings, I disagree with some settings used inblack. Now that the Django project usesblack, they reduced the maximum line length from 119 to 88. This in my opinion Something else, but less important is quoting. Since Python does not distinguish between single and double I fully agree that some parts of the code shall be reformatted, for instance the way we declare models and forms. |
BetaWas this translation helpful?Give feedback.
All reactions
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
For what it's worth, you can set line length in black with the You can also disable the quoting behavior with the As an aside, though, the point of black is specifically to remove personal style from shared projects. I'd suggest this is an necessary step for any open sourced collaborative project. |
BetaWas this translation helpful?Give feedback.
All reactions
👍 1
-
Exactly, that along with the approach of just formatting some parts of the code at once (instead of all of it) we can improve the code readability one step at a time. |
BetaWas this translation helpful?Give feedback.
All reactions
-
Just found out that conversation and thinking about raising a new discussion to use |
BetaWas this translation helpful?Give feedback.
All reactions
-
I've been using Ruff for sometime now and agree it's so much faster locally and in CI. |
BetaWas this translation helpful?Give feedback.
All reactions
-
Yeah I've been meaning to raise it. We've been implementing the ruff linting, so we should do ruff formatting as well given the compatibility with black. |
BetaWas this translation helpful?Give feedback.
All reactions
This discussion was converted from issue #7124 on October 04, 2021 09:26.