How to ignore Pylance type checking on notebooks?
Asked Answered
C

3

7

I have a python project in which I have python files and notebooks.

I use strict typing in my project but I would like to remove it only on notebooks. I use VScode with setting:

"python.analysis.typeCheckingMode": "strict"

I know how to ignore type on a python file:

type ignore python file

But it seems it does not work on notebooks: type ignore notebook

I get the following type error:

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

enter image description here

How can I ignore type checking on notebooks ?

Clyde answered 17/5, 2022 at 8:23 Comment(0)
S
8

That is a Pylance error.

You can create a pyrightconfig.json file at the root of your workspace and define the files to be exclude-d from analysis or completely ignore-d:

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

You can even list up specific filenames:

{
    "ignore": [
        "notimportant.ipynb",
        "test.ipynb",
    ],
}

Historical Notes:

It initially didn't work for 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.

That Github issue has since been resolved the fix was deployed as part of pylance 2022.8.51: https://github.com/microsoft/pylance-release/blob/main/CHANGELOG.md#2022851-31-august-2022-prerelease

Notable changes:

If it somehow still does not work, check the version of pylance on your VS Code.

Sibilate answered 17/5, 2022 at 10:28 Comment(1)
This isn't working for me as of VSCode 1.88.1 + Pylance v2024.4.1.Peggie
P
1

Gino's solution doesn't work for me as of VSCode 1.88.1 + Pylance v2024.4.1.

What worked for mewas adding the following to my pyproject.toml:

[tool.pyright]
exclude = ["**/*ipynb"]
Peggie answered 17/4 at 17:36 Comment(1)
Tracking bug: github.com/microsoft/vscode-jupyter/issues/15560Peggie
F
0

Gino's solution using pyrightconfig.json used to work for me but recently stopped working. I'm running Pylance v2024.7.1 with:

Version: 1.91.0 (Universal)
Commit: ea1445cc7016315d0f5728f8e8b12a45dc0a7286
Date: 2024-07-01T18:54:18.541Z
Electron: 29.4.0
ElectronBuildId: 9728852
Chromium: 122.0.6261.156
Node.js: 20.9.0
V8: 12.2.281.27-electron.0
OS: Darwin arm64 23.4.0
Fussy answered 16/7 at 13:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.