VS Code python 使用笔记之 linting

版权声明:本文为博主原创文章,如能帮助到各位,荣幸之至,欢迎转载。 https://blog.csdn.net/m0_37639589/article/details/82564363

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.

通用语法分析设置

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

Feature Setting
(python.linting.)
Default value
Linting in general enabled true
Linting on file save lintOnSave true
Maximum number of linting messages maxNumberOfProblems 100
Exclude file and folder patterns ignorePatterns [".vscode/*.py", "**/site-packages/**/*.py"]

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

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

定制句法分析器

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

Linter Package name for pip install command Default state True/false enable setting
(python.linting.)
Arguments setting
(python.linting.)
Custom path setting
(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
Pep8 (pycodestyle) pep8 Disabled pep8Enabled pep8Args pep8Path
prospector prospector Disabled prospectorEnabled prospectorArgs prospectorPath
pylama pylama Disabled pylamaEnabled pylamaArgs pylamaPath

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

"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 category Description VS Code category mapping Applicable setting
(python.linting.)
Convention (C) Programming standard violation Information (green underline) pylintCategorySeverity.convention
Refactor (R) Bad code smell Hint (light bulbs) pylintCategorySeverity.refactor
Warning (W) pylintCategorySeverity.warning Warning Python-specific problems
Error (E) pylintCategorySeverity.error Error (red underline) Likely code bugs
Fatal (F) pylintCategorySeverity.fatal Error An error prevented further Pylint processing

Default Pylint rules

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

扫描二维码关注公众号,回复: 5275566 查看本文章

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 研究一下就差不多了,其他的几种句法分析器都不怎么用。需要时再研究,要把时间用在刀刃上。

猜你喜欢

转载自blog.csdn.net/m0_37639589/article/details/82564363
今日推荐