VSCode中使用Pylint检查python代码

为什么使用lint

在日常开发中,不同开发人员会写下不同风格的代码,导致代码可维护性变差,为了解决风格不一致问题,我们可以制定代码规范,让开发人员都遵守同样的规范编写代码。在开发过程中,部分代码存在质量问题,这个时候需要code review的方式,人工检查一些可能的错误。然而,在实际开发中,开发规范仍然严重依赖开发人员的自觉性,还是会出现一些不合规范的现象,在人员发生变化的时候,代码规范需要再次的普及和培训,也浪费了很多时间。代码质量的检查也是严重依赖开发人员review的细致程度,实际上还是有很多bad code引入到工程中。如何自动化的完成上述的工作,在更大程度上提高代码风格一致性和代码质量呢?

lint可以在代码编写期,提交代码期,或者打包部署的时候自动检查代码,它可以识别并自动修改部分错误,比如检测没有初始化或者没有定义的变量,检查调用未定义的方法,自动补齐或者删除行末的分号等。不同于格式化,格式化仅仅能改变代码文本的展示,而lint可以发现代码中的一些错误,提升代码规范和质量。在单人开发或者团队开发的情况下,lint都是很有好处的,它帮助我们写出更好的代码。节省了学习代码规范的时间,避免了开发交接产生的代码规范变化,提升了代码质量,使得code review可以更加专注于代码逻辑,而不是把时间花在检查代码规范或者一些明显的错误上。本文主要讲解在VSCode中怎样lint python代码。

VSCode支持哪些python linter

Linter pip install命令或者pip3 install命令 默认状态 开关设置(python.linting.) 参数设置(python.linting.) 自定义路径设置(python.linting.)
Pylint(default) pylint Enabled pylintEnabled pylintArgs pylintPath
Flake8 flake8 Disabled flake8Enabled flake8Args flake8Path
mypy mypy Disabled mypyEnabled mypyArgs mypyPath
pydocstyle pydocstyle Disabled pydocstyleEnabled pydocstyleArgs pydocstylePath
pycodestyle (pep8) pycodestyle Disabled pycodestyleEnabled pycodestyleArgs pycodestylePath
prospector prospector Disabled prospectorEnabled prospectorArgs prospectorPath
pylama pylama Disabled pylamaEnabled pylamaArgs pylamaPath
bandit bandit Disabled banditEnabled banditArgs banditPath

VSCode支持很多linter,默认使用的是Pylint,python.linting.pylintEnabled控制开关,python.linting.pylintArgs设置参数,python.linting.pylintPath自定义路径。

配置Pylint

代码VSCode,macOS系统中输入cmd+shift+P(windows系统输入ctrl+shift+P),打开命令面板,输入select linter

image-20201007171027121

点击确认后,出现新的面板,选择pylint

右下角出现弹框,提示没有安装pylint,点击install

VSCode中使用Pylint检查python代码_第1张图片

.vscode文件夹下,VSCode自动生成了settings.json文件,文件内容为:

{
    "python.pythonPath": "/usr/local/bin/python3",
    "python.linting.pylintEnabled": true,
    "python.linting.enabled": true
}

“python.pythonPath”: "/usr/local/bin/python3"是python解释器的路径,可以通过命令面板里select interpreter来修改,“python.linting.pylintEnabled”: true代表pylint已经开启,“python.linting.enabled”: true代表lint开启

Pylint配置好了,我们可以试试

Pylint尝试

我们写下两行简单的代码,pylint出现红色波浪线,这代表代码有错误出现,鼠标悬浮直接可以看到,print后面需要跟上括号,pylint给出了修改建议。可以发现,Pylint使用是非常方便的,在代码保存后,它会自动检查代码问题,并指出具体的问题点,很多问题都可以迅速地解决。它提高了我们的代码规范程度,提前发现了代码问题,并且没有花费额外的开发时间,可以说lint是非常有用的工具了。

VSCode中使用Pylint检查python代码_第2张图片

Pylint检查规则

Pylint把检查出的问题分为五个级别,E(Error)和F(Fatal)默认开启,C(Convention)和R(Refactor)默认关闭,W(Warning)信息部分开启部分关闭

  • Enable all Error (E) and Fatal (F) messages. 开启错误和fatal信息
  • Disable all Convention © and Refactor ® messages. 关闭Convention和Refactor信息
  • Disable all Warning (W) messages except the following: 关闭warning信息(以下除外)
    • unreachable (W0101): Unreachable code (不可触达的代码)
    • duplicate-key (W0109): Duplicate key %r in dictionary (字典里面重复key)
    • unnecessary-semicolon (W0301): Unnecessary semicolon (不必要的分号)
    • global-variable-not-assigned (W0602): Using global for %r but no assignment is done (全局变量没有赋值)
    • unused-variable (W0612): Unused variable %r (未使用的变量)
    • binary-op-exception (W0711): Exception to catch is the result of a binary “%s” operation (二进制操作异常)
    • bad-format-string (W1302): Invalid format string (不合理的格式化)
    • anomalous-backslash-in-string (W1401): Anomalous backslash in string (异常的反斜杠)
    • bad-open-mode (W1501): “%s” is not a valid mode for open (不合理的打开方式)

你可能感兴趣的:(开发环境和工具,python学习,vscode,python,ide)