VS Code python 使用笔记之 linting

VS Code python 使用笔记之 linting

Linting 主要是从句法及格式两方面进行分析。Linting 会在你保存文件时自动分析,你也可在命令面板中输入 Python:Run Linting 手动分析。问题会显示在问题面板,并且编辑器中会有相应的下划线提示,鼠标悬停还能获取相应细节。

VS Code 中 python 的默认句法分析器是 Pylint。不过也能使用其他分析器。在命令面板中使用 Python: Select Linter 可以选择其他句法分析器。如果你的 python 环境中没有安装相关的包,就根据相关提示安装极客。

To enable other linters, use the Python: Select Linter command, which again prompts you to install required packages in your selected environment for the linter in question.

通用语法分析设置

这下面是通用句法分析设置:

FeatureSetting
(python.linting.)
Default value
Linting in generalenabledtrue
Linting on file savelintOnSavetrue
Maximum number of linting messagesmaxNumberOfProblems100
Exclude file and folder patternsignorePatterns[".vscode/*.py", "**/site-packages/**/*.py"]

有时如果觉得烦的话可以使用 Python: Enable 命令关了句法分析。

看上表中的 lintOnSave 在文件保存是自动进行句法分析,而如果你将自动保存文件选项打开,这两个的结合就可以快速得到代码句法分析反馈,这个实用。

定制句法分析器

下表是对 python linters 使用设置的基本总结,记住仅 Pylint 是 python 中默认可用的。想要了解单独的设置,看 Linter settings reference。我建议不要开多了,默认的 Pylint 就不错,开多了也很烦。还有就是你如果开了其他语法分析器,那么设置关了之后,需要重启 VS Code 才能发挥作用。

LinterPackage name for pip install commandDefault stateTrue/false enable setting
(python.linting.)
Arguments setting
(python.linting.)
Custom path setting
(python.linting.)
Pylint (default)pylintEnabledpylintEnabledpylintArgspylintPath
Flake8flake8Disabledflake8Enabledflake8Argsflake8Path
mypymypyDisabledmypyEnabledmypyArgsmypyPath
pydocstylepydocstyleDisabledpydocstyleEnabledpydocstyleArgspydocstylePath
Pep8 (pycodestyle)pep8Disabledpep8Enabledpep8Argspep8Path
prospectorprospectorDisabledprospectorEnabledprospectorArgsprospectorPath
pylamapylamaDisabledpylamaEnabledpylamaArgspylamaPath

具体如何设置参数,参考下例:

"python.linting.pylintArgs": ["--reports", "12", "--disable-msg", "I0011"],
"python.linting.flake8Args": ["--ignore=E24,W504", "--verbose"]
"python.linting.pydocstyleArgs": ["--ignore=D400", "--ignore=D4"]

Note that if a top-level element is a single value, as delineated by quotation marks or braces, is still a single item in the list even if the value itself contains spaces.

Pylint

Pylint 的提示消息是按下表分类的,而这些分类会相应映射到 VS Code 的分类中。当然你可以改变这种映射。

Pylint categoryDescriptionVS Code category mappingApplicable setting
(python.linting.)
Convention (C)Programming standard violationInformation (green underline)pylintCategorySeverity.convention
Refactor (R)Bad code smellHint (light bulbs)pylintCategorySeverity.refactor
Warning (W)pylintCategorySeverity.warningWarningPython-specific problems
Error (E)pylintCategorySeverity.errorError (red underline)Likely code bugs
Fatal (F)pylintCategorySeverity.fatalErrorAn error prevented further Pylint processing

Default Pylint rules

下面写道 Pylint 对于大部分人来说都挺爽的。这部分内容认真看,因为如果 Pylint 让你不爽,你就可以修改。

Python in Visual Studio code is configured by default to use a set of linting rules that are friendly to the largest number of Python developers:

  • Enable all Error (E) and Fatal (F) messages.
  • Disable all Convention (C) and Refactor (R) messages.
  • Disable all Warning (W) messages except the following:
    • unreachable (W0101): Unreachable code
    • duplicate-key (W0109): Duplicate key %r in dictionary
    • 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

These rules are applied through the following default arguments passed to Pylint:

--disable=all --enable=F,E,unreachable,duplicate-key,unnecessary-semicolon,global-variable-not-assigned,unused-variable,binary-op-exception,bad-format-string,anomalous-backslash-in-string,bad-open-mode

These arguments are passed whenever the python.linting.pylintUseMinimalCheckers is set to true (the default). If you specify a value in pylintArgs or use a Pylint configuration file (see the next section), then pylintUseMinimalCheckers is implicitly set to false.

For the complete list of Pylint messages, see readable-pylint-messages (GitHub).

命令行参数和配置文件

See Pylint command line arguments for general switches. Command line arguments can be used to load Pylint plugins, such as that for Django:

"python.linting.pylintArgs": ["--load-plugins", "pylint_django"]

Options can also be specified in a pylintrc or .pylintrc options file in the workspace folder, as described on Pylint command line arguments.

To control which Pylint messages are shown, add the following contents to an options file:

[MESSAGES CONTROL]

# Enable the message, report, category or checker with the given id(s). You can
# either give multiple identifier separated by comma (,) or put this option
# multiple time.
#enable=

# Disable the message, report, category or checker with the given id(s). You
# can either give multiple identifier separated by comma (,) or put this option
# multiple time (only on the command line, not in the configuration file where
# it should appear only once).
#disable=

You can easily generate an options file using Pylint itself:

pylint --generate-rcfile > .pylintrc

The generated file contains sections for all the Pylint options, along with documentation in the comments.

Pylint 研究一下就差不多了,其他的几种句法分析器都不怎么用。需要时再研究,要把时间用在刀刃上。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

半美人

动力源于鼓励

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值