Live data from Hacker News

Format Python Code Using YAPF

leimao.github.io

1–10 of 67 posts

Re: Format Python Code Using YAPF

#3
I've used Yapf for a year, then switched to Black when it appeared.

Seriously, use Black. I my experience, and to my taste, it works perfectly all the time, and the result is beautiful.

It's also pep8 compliant, for the parts of pep8 that are concerned by a reformatter.

Re: Format Python Code Using YAPF

#5
post #2

I thought the community standardized on black

black is opinionated. If you disagree with black's opinions then a negotiable formatter like YAPF might be better. I personally love black though.

Black is opinionated enough that you will occasionally mumble about it, but good enough that you will keep using it anyway. That's a remarkable achievement for a formatter. For me it just took a little to get used to double-quoted strings.

Rust's "cargo fmt" tool is similarly good.

Re: Format Python Code Using YAPF

#7
post #2

I thought the community standardized on black

It doesn't really matter which standard you use imho, as long as you automate the tedious task of code formatting to arbitrary standards. If it's something the computer can do let it do it don't waste your time on it. This goes especially in pull requests where dedicated focus to trivial things like formatting often distracts from the real things that need review, like code architecture.

Re: Format Python Code Using YAPF

#8
post #2

I thought the community standardized on black

Black is just slightly too opinionated. It gives you configuration options for benign things such as numeric separators, but it flat out refuses to allow tab indent, which a fairly significant part of the python community uses.

I tried to PR a --use-tabs flag but the PR was rejected without comments. Had to fork Black to be able to use it. Tan is a drop-in replacement that allows --use-tabs (and use-tabs = true in pyproject.toml).

https://github.com/jleclanche/tan

Re: Format Python Code Using YAPF

#9

I've used Yapf for a year, then switched to Black when it appeared. Seriously, use Black. I my experience, and to my taste, it works perfectly all the time, and the result is beautiful. It's also pep8 compliant, for the parts of pep8 that are concerned by a reformatter.

From the experience in our code base, yapf produces higher-quality results, but is much slower.

Black removes any additional parentheses added to enable a line split at a reasonable position (such as "and"). With those removed, black has to split at weird locations like at a function call.

But switching to black just for the speed increase is reasonable.

Re: Format Python Code Using YAPF

#10

I've used Yapf for a year, then switched to Black when it appeared. Seriously, use Black. I my experience, and to my taste, it works perfectly all the time, and the result is beautiful. It's also pep8 compliant, for the parts of pep8 that are concerned by a reformatter.

Black produces code with more lines than yapf. Sure sometimes yapf makes very weird decision (especially when using a dictionary litteral as an argument to a function) but at least it does not use 4 lines for a list with 2 items that could have fitted on one line.
Post reply on HN