How to ignore Pylance type checking on notebooks?

Viewed 615

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 ?

1 Answers

That is a Pylance error.

Supposedly, you should be able to 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",
    ],
}

Unfortunately, it doesn't work as of now 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.

It does work for regular *.py files.

You can try following that Github issue and wait for a fix. Or, the temporary workaround is to add # type: ignore or # pyright: ignore on the specific line raising the error (yes, it is troublesome that it can't ignore the entire file):

VS Code screenshot with error ignored

Related