Python analyzer detects inconsistency between declared types and the code itself

Hello Python developers,

The Python analyzer provides great type checking rules thanks to its type inference engine. It deduces some of the types by relying on Typeshed type stubs. These stubs use the standard python typing module to define each builtin type as well as types for a few well known libraries such as Flask for example.
This typing module can also be used by you to inform anyone reading the code what is the expected type of variables or function returns. This is definitely helping to maintain the code.

We decided to use these data available in the code to raise dedicated code smell issues when type annotations and the code contradict each other.

Here are the 3 rules looking for such type inconsistencies:

  • S5886: Function return types should be consistent with their type hint (Major)
  • S5864: Type checks shouldn’t be confusing (Major)
  • S5890: Values assigned to variables should match their type annotations (Major)

For more information, you can check the changelog.

These features are already available on SonarCloud :sonarcloud: and will be included in SonarQube 8.5 :sonarqube: