# Switching to using a universal code formatter

**URL:** <https://discourse.beets.io/t/switching-to-using-a-universal-code-formatter/1431>\
**Category:** Development\
**Created:** [July 25, 2020, 4:53am UTC](https://discourse.beets.io/t/switching-to-using-a-universal-code-formatter/1431 "2020-07-25T04:53:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jtpavlock](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.beets.io/jtpavlock/32/443_2.png) [@jtpavlock](https://discourse.beets.io/u/jtpavlock)\
**Post date:** [July 25, 2020, 4:53am UTC](https://discourse.beets.io/t/switching-to-using-a-universal-code-formatter/1431/1 "2020-07-25T04:53:17Z")

</div>

The title makes it sound like there may be several options, but I think [black](https://github.com/psf/black) is the de facto.

So, why should we use black?

- Consistent code base
- Never have to worry about formatting ever again, and it looks good
- Effectively solves about 95% of flake8 errors for you. No more annoyance trying to figure out why flake8 is yelling at you.
- Consistent format =\> smaller diffs =\> smaller code reviews

Basically, people are allowed to write code however they want, and then before they commit, just run it through black (either manually or via a pre-commit git hook) and it will take care of any formatting for you.

As far as implementation, the recommended way is to just do one large commit that re-formats the entire codebase, and then use [git-hyper-blame](https://commondatastorage.googleapis.com/chrome-infra-docs/flat/depot_tools/docs/html/git-hyper-blame.html) or [`git blame --ignore-rev` and co](https://github.com/psf/black#migrating-your-code-style-without-ruining-git-blame) to ignore that commit and still be able to see who wrote what.

Thoughts?

Also, if you got the time [here’s a cool talk from the creator of black, Lukasz Langa](https://www.youtube.com/watch?v=esZLCuWs_2Y) (who also is a core python dev)

---

<div class="post-metadata">

**Author:** ![arogl](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.beets.io/arogl/32/455_2.png) [@arogl](https://discourse.beets.io/u/arogl)\
**Post date:** [July 25, 2020, 6:40am UTC](https://discourse.beets.io/t/switching-to-using-a-universal-code-formatter/1431/2 "2020-07-25T06:40:19Z")

</div>

The big question is do we accept black’s default or add some of our style changes:

–skip-string-normalization  
–line-length = 79

I do have diffs for comparison of

black  
black --skip-string-normalization  
black --skip-string-normalization --line-length = 79  
autopep8 --ignore E241,E731,W503,F405,901,FI12,FI14,FI15,FI50,FI51,FI53 [**PR 3673**](https://github.com/beetbox/beets/pull/3673)

---

<div class="post-metadata">

**Author:** ![jtpavlock](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.beets.io/jtpavlock/32/443_2.png) [@jtpavlock](https://discourse.beets.io/u/jtpavlock)\
**Post date:** [July 25, 2020, 2:30pm UTC](https://discourse.beets.io/t/switching-to-using-a-universal-code-formatter/1431/3 "2020-07-25T14:30:32Z")

</div>

Personally, I’m a fan of the defaults. I think the guys over at black put a lot of awesome work and thought into defining the default rules.

---

<div class="post-metadata">

**Author:** ![adrian](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.beets.io/adrian/32/4_2.png) [@adrian](https://discourse.beets.io/u/adrian)\
**Post date:** [July 25, 2020, 4:18pm UTC](https://discourse.beets.io/t/switching-to-using-a-universal-code-formatter/1431/4 "2020-07-25T16:18:10Z")

</div>

I would strongly prefer to keep at least `--line-length=79` if we do this. I’m less familiar with the effects of `--skip-string-normalization` but almost certainly have less of an objection there. 😃

---

<div class="post-metadata">

**Author:** ![jtpavlock](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.beets.io/jtpavlock/32/443_2.png) [@jtpavlock](https://discourse.beets.io/u/jtpavlock)\
**Post date:** [July 25, 2020, 4:30pm UTC](https://discourse.beets.io/t/switching-to-using-a-universal-code-formatter/1431/5 "2020-07-25T16:30:07Z")

</div>

> [@adrian](#):
>
> I’m less familiar with the effects of `--skip-string-normalization` but almost certainly have less of an objection there.

Basically, black uses " over ’ for quotes. [See here, for their explanation](https://black.readthedocs.io/en/stable/the_black_code_style.html?#strings).

`--skip-string-normalization` skips this check, and thus won’t auto format ’ into ".

> If you are adopting _Black_ in a large project with pre-existing string conventions (like the popular [“single quotes for data, double quotes for human-readable strings”](https://stackoverflow.com/a/56190)), you can pass `--skip-string-normalization` on the command line. This is meant as an adoption helper, avoid using this for new projects.

---

<div class="post-metadata">

**Author:** ![jtpavlock](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.beets.io/jtpavlock/32/443_2.png) [@jtpavlock](https://discourse.beets.io/u/jtpavlock)\
**Post date:** [July 28, 2020, 3:55am UTC](https://discourse.beets.io/t/switching-to-using-a-universal-code-formatter/1431/6 "2020-07-28T03:55:57Z")

</div>

[There’s now a PR for this.](https://github.com/beetbox/beets/pull/3694)
