Skip to content
This repository was archived by the owner on Apr 14, 2022. It is now read-only.

VSCode Python Extention giving undefined variable warnings for defined variables in 2020.2.63072 #1908

Closed
meetheed opened this issue Feb 14, 2020 · 12 comments · May be fixed by #1999
Closed
Assignees
Labels
bug Something isn't working feature: linter

Comments

@meetheed
Copy link

Good Afternoon,
i think there may be an bug on the latest release of the Python extention for VSC that results in warnings being given suggesting varibables are undefined when the variables are defined.

ive been using VSCode with the python extention for a while and after updating the python extention today (to 2020.2.63072) i now have 34 warnings in my code for undefined variables. my problem is that they are all defined, the code works as it did before i updated the extention and as i would expect it to.

if i revert the python extention back to version 2020.1.58038 i dont get the warnings, update from that to 2020.2.62710 or 2020.2.63072 and both are giving me warnings.

Ive tried removing all extentions and reinstalling VSC, all without success. Im currently running VSC with only one user installed extention, that is the Python 2020.2.63072 extention.

Below is an example piece of code that throws such a warning for me . in my project i use similar methods whereby i run certain code depending on the environment in which it is run (its intended to run on a raspberry pi with camera, but can be run on windows, for pi i import the picamera modules and support capturing images with the camera, for windows that feature is disabled but the rest of the code runs). The code below reproduces what i think is the bug.

import sys

if sys.platform == 'linux':
    aVariable="Hello Linux"
    print(aVariable) #WARNING THAT aVariable IS UNDEFINED HERE

else:
    aVariable="Hello Other"
    print(aVariable) #NO WARNING HERE, BUT IM WRITING THIS ON WINDOWS

With that code, VSC lists a warning that aVariable is undefined within the linux check, (line 5) . There is no such warning given at line 9 which would trigger if not linux. as i mention above if i move back to an older version of the Python extention this warning is not given.

Im running VSC on Windows 10, and my suspicion is that where i have variables declared and referenced only after a given condition is met, then if that condition is not met within the environment for which i am writing the code, the warnings are generated. e.g. within the if statement to check if this is linux, i get an undeclared variable warning if im writing the code on a windows machine. i dont believe this should happen, and doesnt happen on older versions of the extention.

i have noted that when i switch versions on this extention, the MPLS is redownloaded in the background. im not sure how to obtain the version number for MPLS to be able to post here? My appologies, im new to CSV and Python

Environment data

  • VS Code version: 1.42.1
  • Extension version (available under the Extensions sidebar): 2020.2.63072
  • OS and version: Windows 10 Pro - 1903
  • Python version (& distribution if applicable, e.g. Anaconda): 3.8.1 32 Bit
  • Type of virtual environment used (N/A | venv | virtualenv | conda | ...): n/a
  • Relevant/affected Python packages and their versions: n/a
  • Relevant/affected Python-related VS Code extensions and their versions: Python 2020.2.63072
  • Jedi or Language Server? (i.e. what is "python.jediEnabled" set to; more info How to update the language server to the latest stable version vscode-python#3977): MPLS
  • Value of the python.languageServer setting: XXX
@karthiknadig karthiknadig transferred this issue from microsoft/vscode-python Feb 18, 2020
@jakebailey
Copy link
Member

As far as I know, we haven't made any changes surrounding this, but it seems like some of the platform checks which we use when processing stubs may be getting applied incorrectly. In general in real python code, we don't enforce platform checks.

Undefined variable checking is a second pass over the code, so the first pass may not be walking everything.

@jakebailey jakebailey added the bug Something isn't working label Feb 18, 2020
@ericywl
Copy link

ericywl commented Mar 31, 2020

Same problem here in OSX, latest VSCode version with jedi disabled.

@MikhailArkhipov
Copy link

MikhailArkhipov commented Apr 17, 2020

Here linter walks both if and else bodies. However, analysis does not currently preserves all assignments and only remembers the last one. So the assignment inside else wins and then linter, which runs as a separate pass, sees that variable is assigned below print since assignment inside if is lost. Another possible case that linter is not using same AST traversal logic as the main analysis

@LiteApplication
Copy link

Same on Arch Linux and Code - OSS

Good luck 😁

@andiaa734
Copy link

andiaa734 commented Jan 19, 2021

Ok, wow, today is 19.01.2021 and i ran into this issue. A lot of Variables are marked as not defined and Debugging is not possible through this. Will this issue ever be resolved?

@LiteApplication
Copy link

It left all by itself for me 👍

@bioe007
Copy link

bioe007 commented Apr 7, 2021

Chronic problem here, doesn't even require conditional expressions to be involved. Simple class methods with local variables trigger these warnings..

Sometimes just deleting/re-pasting a line in the file fixes the problem.

@SnoozingSimian
Copy link

The issue still persists, on macOS Big Sur 11.4.

@anoushk1234
Copy link

my issue was fixed after i converted all indentation to spaces

@Porgrammer
Copy link

I am still seeing this bug on windows. Now it also throws proper errors that stop the code.
The code was running fine just last week, and I have not changed anything. Now it doesn't run. Debugging the problems and running some cells manually (as opposed to "Run below" all from the top sometimes helps.

FYI, it's a .py file with Jupyter notebook style markdown and cells.

@jakebailey
Copy link
Member

This repo is no longer maintained, and will soon be deprecated. https://devblogs.microsoft.com/python/python-in-visual-studio-code-september-2021-release/#microsoft-python-language-server-end-of-life-november-2021

You should be seeing warnings of this if you still have your language server set to "Microsoft".

@judej
Copy link

judej commented Apr 13, 2022

Thank you for the report. This project is no longer active. If you are using the Python language server in VSCode, the language server you are using is Pylance and please the http://github.com/microsoft/pylance-release to report issues.

If you are using Visual Studio, then, please use http://github.com/microsoft/PTVS to report issues.

devblogs.microsoft.com/python/python-in-visual-studio-code-september-2021-release/#microsoft-python-language-server-end-of-life-november-2021

thank you

@judej judej closed this as completed Apr 13, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working feature: linter
Projects
None yet
Development

Successfully merging a pull request may close this issue.