SonarCloud detects wrong list or dictionary access leading to IndexError/KeyError

Hello Python developers,

Python analysis has been upgraded to detect bugs that make your application crash with KeyError or IndexError exceptions.

The 3 added rules are:

  • S6464: Non existent dictionary keys should not be accessed
  • S6465: Unpacking should have the same number of elements of the iterable
  • S6466: Accessing list elements should not trigger an IndexError

Here is an example of a mistake that can be detected with such rules:

The key server_platform doesn’t exist in the certificate dictionary and we’ll lead to a KeyError if the condition on line 473 is true.

These rules are available now on SonarCloud.io and will be part of SonarQube 9.8.

Alex

4 Likes

Wow, thanks for these detailed elements!