pythonvisual-studio-codejupyter-notebookpylance

How to ignore Pylance type checking on notebooks?


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 ?


Solution

  • 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.