Writing Python properly tends to break the coding style (especially the width grows), so I decided to introduce a code formatter and code checker. I think there are a lot of articles like this, but I will write them as a memorandum of my own.
I'm running Python 3.8.2 on Windows 10 WSL using pyenv.
I first tried autopep8 and yapf, referring to this article https://www.kimoton.com/entry/20181223/1545540702. Both are easy to install with pip:
pip install autopep8
pip install yapf
Looking at the code example tested in the article and its format result, it seems that it is different whether to insert a line break every time at the comma separated part.
It's obvious when you try it, especially with long arrays:
test.py
a = [1, 2, 3, 45, 6789, 998244353, 1000000007, 1000000009, 18446744073709551615, 1, 2, 3, 45, 6789, 998244353, 1000000007, 1000000009, 18446744073709551615, 123456789]
$ autopep8 -a test.py
a = [
1,
2,
3,
45,
6789,
998244353,
1000000007,
1000000009,
18446744073709551615,
1,
2,
3,
45,
6789,
998244353,
1000000007,
1000000009,
18446744073709551615,
123456789]
$ yapf test.py
a = [
1, 2, 3, 45, 6789, 998244353, 1000000007, 1000000009, 18446744073709551615,
1, 2, 3, 45, 6789, 998244353, 1000000007, 1000000009, 18446744073709551615,
123456789
]
(The argument -a
in autopep8 is an abbreviation for --aggressive
. If you don't specify this, it doesn't seem to format to shorten the line length.)
As you can see, autopep8 inserts line breaks for each element, while yapf seems to insert line breaks conservatively as long as the length limit is not exceeded. As a personal preference, I decided to use yapf because I thought it would be verbose and difficult to read if I could insert line breaks for each element.
The result of formatting the file <filename>
with yapf <filename>
is printed to standard output.
yapf seems to have fine control over the style, but here I'd like to use the default settings.
Here are some basic options for using the formatter (excerpt from yapf --help
):
-d, --diff print the diff for the fixed source
-i, --in-place make changes to files in place
-r, --recursive run recursively over directories
--With the --diff
option, only the changes will be pointed out in diff format.
--Yapf outputs the entire formatted file to standard output by default, but with the --in-place
option, the file is overwritten with the format result.
--With the --recursive
option, you can format all files under a directory (including inside lower directories).
I decided to use flake8 as the code checker. flake8 can also be installed with pip.
pip install flake8
For example, if you apply the long sideways test.py
to flake8, it says" the line is too long ", but you can confirm that it can be solved by applying yapf:
$ flake8 test.py
test.py:1:80: E501 line too long (167 > 79 characters)
$ yapf -i test.py
$ flake8 test.py
$
Note that flake8 seems to be able to check the entire folder and below without adding options such as -r
.
For example, in a two-dimensional array (list), you may want to align the commas line by line:
2darray.py
a = [
[ 1, -2, 3],
[-4, 5, -6],
[ 7, -8, 9]
]
For alignment, I intentionally put a space immediately after [
, but this causes an error when I run it on flake8:
$ flake8 2darray.py
2darray.py:2:6: E201 whitespace after '['
2darray.py:4:6: E201 whitespace after '['
According to the flake8 documentation (https://flake8.pycqa.org/en/latest/user/violations.html#in-line-ignoring-errors), you can ignore the error by adding the comment # noqa: E201
to the end of the line.
(In this case I want to ignore the same error over multiple lines, but from the documentation it seems that I can't do that unless I put an exception in the whole file ...)
a = [
[ 1, -2, 3], # noqa: E201
[-4, 5, -6], # noqa: E201
[ 7, -8, 9] # noqa: E201
]
$ flake8 2darray.py
(Nothing is displayed)
However, if you apply yapf in this state, it will still be regarded as an extra blank and will be erased:
$ yapf 2darray.py
a = [
[1, -2, 3], # noqa: E201
[-4, 5, -6], # noqa: E201
[7, -8, 9] # noqa: E201
]
In this case, you can partially disable the format by adding a comment as in yapf docs (unlike flake8, you can also specify in expression or range units. is):
a = [
[ 1, -2, 3], # noqa: E201
[-4, 5, -6], # noqa: E201
[ 7, -8, 9] # noqa: E201
] # yapf: disable
You can now safely disable yapf checking as well.
$ yapf -d 2darray.py
(Nothing is displayed)
Recommended Posts