首页 > 解决方案 > 如何在 VS Code 中查看 Python 诊断严重性规则的默认值?

问题描述

如此处所述,如何查看(或在 JSON 文件中提取)VS Code 中 Python 的诊断严重性规则的默认值? https://github.com/microsoft/pylance-release/blob/main/DIAGNOSTIC_SEVERITY_RULES.md#diagnostic-severity-rules

标签: visual-studio-codepylance

解决方案


如果您只需要查看 VS Code 中的默认值,只需打开 settings.json 文件(用户或工作区),然后一一输入其中的每个键。每个可能的值都应该出现一个弹出窗口,默认值将在描述中带有“默认值”提示。

设置界面

进入设置

查看默认值提示

您还可以检查机器上安装的 Pylance 扩展目录。首先,找到所有 VS Code 扩展的目录

扩展安装在每个用户的扩展文件夹中。根据您的平台,该位置位于以下文件夹中:

  • 视窗%USERPROFILE%\.vscode\extensions
  • 苹果系统~/.vscode/extensions
  • Linux~/.vscode/extensions

转到该文件夹​​,然后查找该ms-python.vscode-pylance*文件夹。

$ cd ~/.vscode/extensions/

extensions$ tree . -L 1 | grep "ms-python.vscode-pylance"
├── ms-python.vscode-pylance-2021.10.0

extensions$ cd ms-python.vscode-pylance-2021.10.0

ms-python.vscode-pylance-2021.10.0$ tree -L 1 .
.
├── CHANGELOG.md
├── LICENSE.txt
├── NOTICE.txt
├── README.md
├── dist
├── images
├── package.json
├── package.nls.json
└── package.nls.ru.json

打开package.json ,在configuration>>下的某个地方,您会发现所有这些规则及其默认值:propertiespython.analysis.diagnosticSeverityOverrides

...
"reportMissingImports": {
    "type": "string",
    "description": "Diagnostics for imports that have no corresponding imported python file or type stub file.",
    "default": "warning",
    "enum": [
            "none",
            "information",
            "warning",
            "error"
    ]
},
"reportMissingModuleSource": {
    "type": "string",
    "description": "Diagnostics for imports that have no corresponding source file. This happens when a type stub is found, but the module source file was not found, indicating that the code may fail at runtime when using this execution environment. Type checking will be done using the type stub.",
    "default": "warning",
    "enum": [
            "none",
            "information",
            "warning",
            "error"
    ]
},
"reportMissingTypeStubs": {
    "type": "string",
    "description": "Diagnostics for imports that have no corresponding type stub file (either a typeshed file or a custom type stub). The type checker requires type stubs to do its best job at analysis.",
    "default": "warning",
    "enum": [
            "none",
            "information",
            "warning",
            "error"
    ]
},
...

默认值在default键中。(请注意,与 settings.json 弹出窗口中的屏幕截图中的默认reportMissingImports匹配。

如果您需要将其提取为 JSON 格式,那已经是一个 JSON 文件。


推荐阅读