Python analysis upgraded to detect 3 new critical Python bugs

Hello Python developers,

We continued our journey to detect Python bugs that will make your application either crash or not behave as you would expect.

Here are the 3 added rules:

  • S6417: Collections should not be modified while they are iterated
  • S5633: Dictionary unpacking should only be done with “mapping” objects
  • S2583: Conditionally executed code should be reachable

On the project Calibre, the rule S2583 detected this issue:

Here, the engine knows for sure that the condition on line 394 always evaluates to false. This is because there is the same condition evaluated on line 390. If there is no “title” provided, the function will stop here and return. If the execution flow reaches line 394, this means that a “title” was provided and so it means that not title will always be false on line 394.

In this other example, the engine detects an incompatible type that will lead to a runtime TypeError:

image

The dictionary unpacking operation requires an object with methods __getitem__ and keys or __getitem__ and __getattr__ which is not the case of the object A is the example.

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

Alex

1 Like