yapf: FInd config that works across different versions
Currently we have in our experimental CI setup (#246) the following failure:
find ./util -iname '*.py' -print0 | xargs -0 -n1 $PWD/util/lintpy.py -f
========================== Starting Command Output ===========================
[command]/bin/bash --noprofile --norc /home/vsts/work/_temp/315cc780-ed8b-4927-be16-919a17390cd3.sh
Lint failed:
yapf -d ./util/docgen/stwg_renderer.py
--- ./util/docgen/stwg_renderer.py (original)
+++ ./util/docgen/stwg_renderer.py (reformatted)
@@ -178,8 +178,8 @@
formatter.noclasses = True
def __init__(self, *extras, style='default', srcfile='', wavejs=False):
- super().__init__(*chain((StwgEscape, RegRef,
- CodeFenceDirective), extras))
+ super().__init__(
+ *chain((StwgEscape, RegRef, CodeFenceDirective), extras))
self.formatter.style = get_style(style)
self.regs = None
self.wavejs = wavejs
##[error]Bash exited with code '123'.
##[section]Finishing: Run Python lint
I use the same version (0.26.0) of yapf locally but I don't see that failure when running it locally. However, I'm running openSUSE Tumbleweed and not Ubuntu 16.04, and Python 3.7.2 instead of 3.5.1. But we should find a way to make yapf independent of those things and give consistent results.
It's even a worse. When I do the changes suggested by the CI version of lint locally, my local version of yapf fails and requires the code to be fixed the other way around:
$ find ./util -iname '*.py' -print0 | xargs -0 -n1 $PWD/util/lintpy.py -f
Lint failed:
yapf -d ./util/docgen/stwg_renderer.py
--- ./util/docgen/stwg_renderer.py (original)
+++ ./util/docgen/stwg_renderer.py (reformatted)
@@ -178,8 +178,8 @@
formatter.noclasses = True
def __init__(self, *extras, style='default', srcfile='', wavejs=False):
- super().__init__(
- *chain((StwgEscape, RegRef, CodeFenceDirective), extras))
+ super().__init__(*chain((StwgEscape, RegRef,
+ CodeFenceDirective), extras))
self.formatter.style = get_style(style)
self.regs = None
self.wavejs = wavejs
Does the CI setup have a .style.yapf in a findable location? It is in the root of the stwg repo so will be found by anything in repo. The only issue would be if yapf has changed its pep8 baseline rules (I assumed that is a big no-no because all code that was reported ok will subsequently fail). If that is going on then we will have to get yapf to dump its entire ruleset (there is an option) and make a .style.yapf with everything enumerated.
- I've reproduce this is another Ubuntu 16.04 instance locally.
- The config is exactly the same, I double-checked with
--style-help - The only significant difference seems to be the Python version, making this a yapf bug. I've opened https://github.com/google/yapf/issues/696 with a reduced testcase, let's see where this leads us. Until then I'll disable yapf for this code section.
@imphil, looks like the yapf bug you opened google/yapf#696 is still open. Can you ping again?
This is unlikely to be ever resolved. Leaving this open for now, and we'll need to look into alternatives to yapf in the future.
Let's pick this one up again.
- It is likely that the underlying issue in yapf isn't relevant for us any more, since we now use Python 3.6+.
- However, yapf seems to be rather unmaintained these days, and there's a strong possibility we run into similar issues again.
- black is an alternative which is stronger in its opinions, but more stable in my experience. Also, other large projects like Mozilla recently switched to it.
- Black is less configurable than yapf (by design), so we'd have to adjust our style guide to effectively say "whatever the formatter does is correct".
- It would be great to have a stable, enforcing formatter working again for Python, we did see a fair amount of style churn in Python files recently.
How is the general feeling in the room: should we go with black to improve the developer experience, at the cost of changing some of our Python style guide?
Do you have any feel for how extensive the diff of black's preferences vs our current style guide is?
I did run black on all of OT's Python sources (excluding files in vendor directories), and here's the result: https://github.com/imphil/opentitan/commit/b4b98a746c5bff5a99b30de3c26cc6aa80df465c
The vast majority of changes are due to the enforcement of a double quotes. [Black can be told to not do that, but it's neither recommended by black authors, nor by me.] On the second place are changes to line continuation within parentheses (e.g. function calls).
The whole style guide is available at https://black.readthedocs.io/en/stable/the_black_code_style.html.
Hmm, maybe we could customise black to allow single quotes (which I think is the usual Python style, no matter what the Black authors say!). I also really dislike it's out-denting indenting algorithm. This seems to be the conventional solution for automatic code formatters, but I've never seen anyone who chooses to
write code (
like
)
that.
Yuck!
Do we really need to enforce a style with a --fix mode? Another option would be to use something less opinionated (like flake8) and require authors to generate one of the many possible formatting options that matches.
First of all, I am not a python guy here :) I agree yapf creates too much diffs across the different systems depending on the python version. But solved it in Pyhon3.6+ . Then, I think we don't have to switch yapf even it is not maintained well. Maybe in the next Python version, it would break. Then we can freely choose alternatives at that time, as formatter is not a must thing to do.
I believe we have now switched to flake8 in CI to circumvent the yapf problems detailed above.
Can we close this issue?
Closing due to inactivity - please reopen if you think this is still relevant.