如何忽略笔记本上的 Pylance 类型检查?

How to ignore Pylance type checking on notebooks?

我有一个 python 项目,其中有 python 个文件和笔记本。

我在我的项目中使用严格类型,但我只想在笔记本上删除它。我使用 VScode 设置:

"python.analysis.typeCheckingMode": "strict"

我知道如何忽略 python 文件的类型:

但它似乎不适用于笔记本:

我收到以下类型错误:

"Type of "y" is partially unknown
Type of "y" is "Unknown | None (pylance)"

如何忽略笔记本的类型检查?

这是一个 Pylance 错误。

据推测,您应该能够在工作区的根目录下创建一个 pyrightconfig.json 文件,并将文件定义为 exclude-d 从分析或完全 ignore-d :

{
    "ignore": [
        "**/*.ipynb",
    ],
}

不幸的是,它目前不适用于 Jupyter Notebooks (.ipynb):
https://github.com/microsoft/pylance-release/issues/2135

This happens because pyright doesn't see the file as a "*.ipynb". The file is being preprocessed (to combine all of the cells) in the notebook by the VS Code Python extension, and the resulting combined file is then passed to pyright for analysis.

The pylance team is actively working on changing the way this works. I'm going to transfer this bug to the pylance-release repo so it gets the attention it deserves.

它确实适用于常规 *.py 文件。

您可以尝试解决 Github 问题并等待修复。或者,临时解决方法是在引发错误的特定行上添加 # type: ignore# pyright: ignore(是的,它不能忽略整个文件很麻烦):