vscode-jupyter icon indicating copy to clipboard operation
vscode-jupyter copied to clipboard

Pylance reportUndefinedVariable when using Jupyter startup script

Open slnc opened this issue 10 months ago • 0 comments

Applies To

  • [X] Notebooks (.ipynb files)
  • [X] Interactive Window and/or Cell Scripts (.py files with #%% markers)

What happened?

Pylance incorrectly warns about undefined variables when they're defined indirectly via %run statements or through /home/$USER/.ipython/profile_default/startup/preamble.py files.

The only sane workaround I've found is to disable the reportUndefinedVariable which, sadly, also disables the valuable true positive cases.

How to reproduce:

  1. Create an .ipython startup script:
mkdir -p /home/$USER/.ipython/profile_default/startup/
echo "import pandas as pd" >> /home/$USER/.ipython/profile_default/startup/preamble.py
  1. Restart VSCode/Jupyter kernel.
  2. In a blank notebook, create a cell with:
pd.DataFrame([])

Expected: Pylance doesn't give any errors because the cell works.

Actual: image

If Pylance can't dynamically check for this (a reason I've read in past issues reporting this same bug), would it be possible to make Pylance ignore certain user-defined undefined variables ("pd", "np", etc) and only for certain (.ipynb) file patterns through a user defined dict?

    "python.analysis.diagnosticSeverityOverrides": {
        "reportUndefinedVariableForFilePatterns": {
           "**/*.ipynb": ["pd", "np"]
        }
    },

It's a bit of a hack, but that would solve all the instances I've found of this problem (1 2 3).

VS Code Version

1.88.1

Jupyter Extension Version

v2024.4.1.

Jupyter logs

No response

Coding Language and Runtime Version

No response

Language Extension Version (if applicable)

No response

Anaconda Version (if applicable)

No response

Running Jupyter locally or remotely?

Local

slnc avatar Apr 17 '24 17:04 slnc