Python Linter

最近项目codebase 迁移到了GoB/Gerrit 的体系中,提交代码后CI 会做code linting 操作并且提示错误信息,最好是在提交前在本地先自检,但目前似乎没有集成本地linting 的功能, 经过观察,可以自己搭建linting 的环境去对任意2次commits 之间改动过的py, yaml or other 文件进行语法,格式的检查。

Lint Steps

为了方便,这里使用python virtualenv, 也可以使用docker 环境,mount整个repo 然后处理. work on a python virtual env, for example virtualenv -p python3 venv:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# activate the venv first
# need to be the same yamllint pylint version as the team use
# different version has different output maybe
pip install yamllint==1.17.0
pip install pylint==2.12.2

# filter and get updated yaml and python file name list
# diff between HEAD~1 HEAD, order matters!
# the between is for endpoint not for range!
YAML_FILES=$(git diff --name-only --diff-filter=ACMR HEAD~1 HEAD | grep -E "(.+\.yaml$|.+\.yml$)" || echo "")
PY_FILES=$(git diff --name-only --diff-filter=ACMR HEAD~1 HEAD | grep -E ".+\.py" || echo "")

# .pylintrc and .yamllint
# should be in the code repo
# -r: no run if input is empty
# xargs default input will be placed at end
echo $PY_FILES | xargs -r pylint -sn --rcfile=.pylintrc >> linter_results_tmp
echo $YAML_FILES | xargs -r yamllint -c .yamllint >> linter_results_tmp

cat linter_results_tmp && rm -f linter_results_tmp

Disable PyLint

You may need to update .pylintrc setting to skip warnings/errors, edit disable line to add error code or statement, for example:

1
disable=C0103,missing-docstring,too-many-branches

Or disabling the pylint check inline:

1
2
if __name__ == '__main__':
run() # click command, pylint: disable=E1120

Or more readable, use the symbolic name:

1
2
if __name__ == '__main__':
run() # click command, pylint: disable=no-value-for-parameter

And disable in a function level, for example:

1
2
3
4
def wont_raise_pylint():
# pylint: disable=W0212
some_module._protected_member()
some_module._protected_member()

And disable in a file or bigger scoup, the following part will be disabled by this rule, you can enable it again:

1
2
3
# pylint: disable=use-implicit-booleaness-not-comparison
...
# pylint: enable=use-implicit-booleaness-not-comparison
0%