如何在 VS Code 中查看 Python 诊断严重性规则的默认值?

How can I see the default values for the Python Diagnostic Severity Rules in VS Code?

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

C:\Users\{UserName}\.vscode\extensions\ms-python.vscode-pylance-2021.10.0\dist\schemas\pyrightconfig.schema.json

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

您还可以检查计算机上安装的 Pylance 扩展目录。首先,find the directory for all your VS Code extensions:

Extensions are installed in a per user extensions folder. Depending on your platform, the location is in the following folder:

  • Windows %USERPROFILE%\.vscode\extensions
  • macOS ~/.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 > properties > python.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 键中。 (注意 reportMissingImports 的默认值与 settings.json 弹出窗口中的屏幕截图相匹配。

这已经是一个 JSON 文件,如果您需要将其解压为 JSON 格式。