skip to Main Content

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


  1. the variable geeks has type list[int], deduced by PyLance static analysis.

    The other error message you get is more descriptive of the reason

    Argument of type "None" cannot be assigned to parameter "__o"
    of type "Iterable[int]" in function "__setitem__" 
    "__iter__" is not present
    

    Adding a None to this list is not allowed.

    If you give geeks the correct type the error is gone

    from typing import List, Union
    
    geeks: List[Union[int, None]] = [6, 0, 4, 1]
    
    Login or Signup to reply.
  2. To 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 of List[int]. I don’t think, it may cause any problems in future, but having warnings and errors just is not great.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search