plone / Products.CMFPlone

The core of the Plone content management system
https://plone.org
GNU General Public License v2.0
255 stars 191 forks source link

PLIP: use an autoformatter to keep our code base (a bit more) consistent #2754

Closed gforcada closed 1 year ago

gforcada commented 5 years ago

PLIP (Plone Improvement Proposal)

Responsible Persons

Proposer: Gil Forcada Codinachs (gforcada)

Seconder: up for grabbing

Abstract

Motivation

tl;dr; efficiency, i.e. once and for all, end up all the formatting discussions and make contributing to Plone even easier.

Plone is transitioning into Python 3 and has decades of history on its shoulders. Linters and styleguides have helped keep some code consistency, but just like tendencies, they keep changing over time, and most of the times are left half implemented.

As everything, it starts small and gets more and more complex over time:

Autoformatters are already mature enough to deal with large code bases. This means, that for the first time ever, we are in a position where we can finally have a single source of truth on how to format our code base.

As usual though, that gets us only up to a certain degree, no functions, methods, classes etc. will get renamed.

Black is a python code autoformatter that has to main promises:

Notice that having the code formatted automatically (by black or whichever other tool) does not necessarily mean that we do not need any code analysis tool anymore. A bare exception handling is a really bad thing that one should never do, code analysis tools will warn about it, auto formatters will never.

Fortunately for us, it is not too much effort to make them cooperate with each other :+1:

Assumptions

Proposal & Implementation

Deliverables

Risks

We keep the current half mess of styleguides and formattings, as well as linting hints on and off here and there.

We hinder contributions from newcomers by making them uneasy on how they are expected to format their contribution.

Participants

ale-rt commented 5 years ago

Well, my favourite tool for this is https://github.com/google/yapf, which is much more configurable and IMHO produces better code. Anyway if we decide to go for black I am all in because I am 100% sure that adopting it will be a big benefit.

jensens commented 5 years ago

I second using black.

@ale-rt if yapf is your choice just configure it in a way its doing the same as black.

But my experience with yapf is its much more difficult to use and, most important, it allows too many options leading to discussions which this PLIP intention is to avoid.

ale-rt commented 5 years ago

If we decide to go black I will switch to black with no problem! My personal evaluation lead to YAPF exactly because it is more configurable, but if the majority picks another tool I will switch to it without any problem. I see much more an advantage to have this PLIP approved with black than having nothing like the current status is. So if everybody loves black, let's go black :)

davisagli commented 5 years ago

+1 for black. The lack of configurability (and thus lack of bike-shed-ability) is a feature.

tisto commented 5 years ago

+1 for black. Though, we might want to wait with that until we get rid of Python 2. Since black does not run on Python 2 we always risk that people who work with Python 2 are not able to run the formatter.

tisto commented 5 years ago

@gforcada the @plone/framework-team approved your PLIP in its meeting today. Please go ahead with the implementation.

jaroel commented 5 years ago

+1 for black. Though, we might want to wait with that until we get rid of Python 2. Since black does not run on Python 2 we always risk that people who work with Python 2 are not able to run the formatter.

Black itself uses python3, but it will format both 2 and 3.

mauritsvanrees commented 5 years ago

In the issue linked by Jens just above, he says we can use black --check . That could replace most of flake8. Not all of it: there are some plone/zope-specific flake8 plugins that we use.

gforcada commented 5 years ago

Yup, there are plenty of things that black does not check(i.e. a bare except: clause for example) that still needs flake8.

But there are quite a few flake8 plugins that can be disabled completely.

jensens commented 5 years ago

Sure, it does not check all. Btw., now the PLIP is approved, what is next?

gforcada commented 5 years ago

Me finding some time to work on the deliverables? :sweat_smile:

gforcada commented 5 years ago

But, please, pretty please, forget about this PLIP for now, we have pretty much more specific and urgent topics to work on (i.e. get 5.2 out :wink: )

idgserpro commented 5 years ago

This will have implications with bobtemplates.plone, used to create packages in the Plone world. Should we try to enforce black there as well? For the sake of an example, if I run black in a newly created package from bobtemplates.plone, without --skip-string-normalization, it would create a big diff:

https://github.com/plone/bobtemplates.plone/blob/abb43795ed611138d451b61d05f0ec78b6e827d1/bobtemplates/plone/addon/setup.py.bob#L16

@MrTango FYI

gforcada commented 1 year ago

This has been applied since the plone/meta project being put in motion this year 2023 🎉