My code is running perfeclty with no errors from python shell but in VS Code IDE it is highlighting geeks[i] = None
as error and giving the above(title) as problem/error.
Python:
geeks = [6, 0, 4, 1]
i = 0
while i < len(geeks):
geeks[i] = None
i += 1
geeks = [x for x in geeks if x is not None]
print(geeks)
Aim of code is to remove all the element from the list .
I know various way by which it could be done but was curious why this happend in VS Code & how to solve this ?
Is there any problem that could occur later ?
the code runs fine but VS Code IDE shows it as error
2
Answers
the variable
geeks
has typelist[int]
, deduced by PyLance static analysis.The other error message you get is more descriptive of the reason
Adding a
None
to this list is not allowed.If you give
geeks
the correct type the error is goneTo solve this, you can just click on curly braces on blue bottom panel, then switch type checking to off.
As far as I’m concerned, this happens because VS code sees you try to assign
None
to an item ofList[int]
. I don’t think, it may cause any problems in future, but having warnings and errors just is not great.