Home > other >  Why do I get error message in python that len is missing when I use the built in len function?
Why do I get error message in python that len is missing when I use the built in len function?

Time:11-14

I am currently working on a python project - windows application / software that has GUI where you can put patient information and so on and all that is going to a local mysql database / table. all that works but my IDE VS code gives 4 error messages and one of the errors is related to "len()" function. I will provide Github link of the entire code plus the error messages so if someone can help, please do so! Thank you!

https://github.com/ethicalduty/Hospital_Management_Software_Windows/blob/main/main_Hospital_Management_Software_Windows_3.11.0.py

I am new to programming so I cannot do much besides trying to find similar solution on google. I have not found anything already resolved so far, thus asking for help here!

CodePudding user response:

The "errors" you are seeing are type checking issues, not real Python errors that are occurring when you run your code. They're warnings that there might be real errors in the code, but you should be able to run it regardless and the code may work fine. Whether there's a real issue may depend on the data the code is processing (it might work for some kinds of data but not others), or there might be inaccuracies in how the type checker is interpreting the code (and so there is no real issue in the code at all).

The issue that has to do with len seems to be saying that your rows = my_cursor.fetchall() statement may assign None to rows, which would cause an error below where you do len(rows). I don't know MySQL's Python bindings, so I'm not sure if the type checker's assumption is correct that a query could cause fetchall to return None (rather than an empty list), but if that's the case, you can easily fix the type checking issue (and the possibly small chance of a real error when you run the code) by checking that rows is not None before you check its length:

    rows = my_cursor.fetchall()
    if rows is not None and len(rows) != 0:
        ...
  • Related