Skip to content
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

Introduce formatter #956

Closed
keisuke-umezawa opened this issue Feb 24, 2020 · 12 comments
Closed

Introduce formatter #956

keisuke-umezawa opened this issue Feb 24, 2020 · 12 comments
Labels
enhancement Change that does not break compatibility and not affect public interfaces, but improves performance.

Comments

@keisuke-umezawa
Copy link
Member

keisuke-umezawa commented Feb 24, 2020

Motivation

If we introduce a formatter to Optuna and just use it, we do not need to think about formatting.

Description

There are famous formatters in python:

  1. autopep8 (current)
  2. Black
  3. yapf

c.f. https://blog.frank-mich.com/python-code-formatters-comparison-black-autopep8-and-yapf/

We need to discuss what formatter is best.

Differences of above formatters

black

  • Use " instead of '.
  • Add trailing commas to expressions that are split by comma where each element is on its own line.
  • Break a line before a binary operator when splitting a block of code over multiple lines.

black vs yapf

c.f. https://news.ycombinator.com/item?id=17155048

  • YAPF would at times not produce deterministic formatting (formatting the same file the second time with no changes in between would create a different formatting); Black treats this as a bug;
  • YAPF would not format all files that use the latest Python 3.6 features (we have a lot of f-strings, there's cases of async generators, complex unpacking in collections and function calls, and so on); Black solves that;
  • YAPF is based on a sophisticated algorithm that unwinds the line and applies "penalty points" for things that the user configured they don't like to see. With a bit of dynamic programming magic it arrives at a formatting with the minimal penalty value. This works fine most of the time. When it doesn't, and surprised people ask you to explain, you don't really know why. You might be able to suggest changing the penalty point value of a particular decision from, say, 47 to 48. It might help with this particular situation... but break five others in different places of the codebase.
@keisuke-umezawa keisuke-umezawa added the feature Change that does not break compatibility, but affects the public interfaces. label Feb 24, 2020
@keisuke-umezawa
Copy link
Member Author

imo, I want to use black as our formatter.

@c-bata
Copy link
Member

c-bata commented Feb 24, 2020

+1 to use black.

I also use black at https://github.com/CyberAgent/cmaes.

@c-bata c-bata added enhancement Change that does not break compatibility and not affect public interfaces, but improves performance. and removed feature Change that does not break compatibility, but affects the public interfaces. labels Feb 24, 2020
@kidrahahjo
Copy link

Flake8 can also be a good formatter at times.
It's not rigid and also it doesn't automatically fix the errors which is often useful.

@hvy
Copy link
Member

hvy commented Mar 1, 2020

Leaving some community statistics that shows how Black is being more adopted.

Stars: https://star-history.t9t.io/#google/yapf&psf/black
Used by: Black has twice as many.

@keisuke-umezawa
Copy link
Member Author

@/here
How can I archive conclusion? 🤔

btw, I will create PR with black.

@hvy
Copy link
Member

hvy commented Mar 5, 2020

I think most of the core members are okay with black, so far. A PR could be a starting point to discuss it more in detail. Just curious how it will be different from #957 ?

@c-bata
Copy link
Member

c-bata commented Mar 6, 2020

In my opinion, we need to avoid conflict with other pull requests as possible as we can. At least, #957 significantly increases the costs of other pull requests.

@keisuke-umezawa
Copy link
Member Author

keisuke-umezawa commented Mar 6, 2020

I created two PRs for applying black to all files with following conditions, because it is better to compare them. Here, string format means converting ' to ".

@hvy
Copy link
Member

hvy commented Mar 9, 2020

Thank you for the different PRs. The actual changes are much easier to see if the string normalizations are separated (as they are different commits). Briefly discussing this with the team, it seems like most are okay with introducing black, and in particular to align to " for strings. One of the purposes of the formatter is to keep a consistent style not having the developer have to think about it. It's of course open for discussion but I think we can proceed with strings being normalized.

However, as pointed out in #956 (comment), this might increase the cost of ongoing PRs. Do you have any suggestion on how to proceed? As most PRs are local enough, I for instance think that splitting up the PR won't benefit that much. We could try to merge larger PRs or long-running PRs prior to introducing the formatter.

@keisuke-umezawa
Copy link
Member Author

keisuke-umezawa commented Mar 10, 2020

However, as pointed out in #956 (comment), this might increase the cost of ongoing PRs. Do you have any suggestion on how to proceed? As most PRs are local enough, I for instance think that splitting up the PR won't benefit that much. We could try to merge larger PRs or long-running PRs prior to introducing the formatter.

That is true. If we want to apply black, we need to think how to do it. Maybe, one of the best chances is just before next release? 🤔

@keisuke-umezawa
Copy link
Member Author

keisuke-umezawa commented Mar 12, 2020

@hvy I opened a new PR to merge changes. #1020

As we discussed, we will apply black as follows:

  1. Apply black with string normalization and 99 chars in a line.
  2. Enable black instead of autopep8.
  3. Check black in circleci.

@hvy
Copy link
Member

hvy commented Mar 17, 2020

I created a PR addressing above comment #1030 (c.f. #1020 (comment)). It has now been merged so let me close this issue. Thanks a lot @keisuke-umezawa for the long running investigation.

@hvy hvy closed this as completed Mar 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Change that does not break compatibility and not affect public interfaces, but improves performance.
Projects
None yet
Development

No branches or pull requests

4 participants