SonarCloud scans Python 3.8 code and detects mistakes related to method signature and visibility

Hello Python developers,

Python 3.8

Python 3.8 was released in Oct. 2019 and since then its support has been expected by some of you. The wait is over: you can use the latest Python features such as the “walrus operator” := and scan your Python 3.8 code in SonarCloud.
The icing on the cake: there is a rule dedicated to “walrus operator” to be sure this new syntax doesn’t make your code more confusing that without it (PEP 572):

  • S5685: Walrus operator should not make code confusing (Code Smell - Minor)

Method Signature and Visibility Rules

Python might seem familiar to developers coming from other object oriented languages, but it has many specificities and conventions: “self” parameter, positional/keyword parameters, private members which are in fact using name mangling, etc…
We upgraded the Python analyzer to avoid such common mistakes when defining method/function and to detect dead code due to class-private members.

Code Smell Detection:

  • S4487: Unread “private” attributes should be removed (Critical)
  • S2638: Method overrides should not change contracts (Critical)
  • S5717: Function parameters’ default values should not be modified or assigned (Critical)
  • S5720: “self” should be the first argument to instance methods (Critical)
  • S2710: The first argument to class methods should follow the naming convention (Critical)
  • S1144: Unused class-private methods should be removed (Major)

    There is a missing “_” in the name making this “_init” method not a constructor but an unused class-private method.
  • S3985: Unused private nested classes should be removed (Major)

Bug Detection:

  • S5719: Instance and class methods should have at least one parameter (Blocker)
  • S5722: Special methods should have an expected number of parameters (Blocker)
  • S5724: Property getter, setter and deleter methods should have the expected number of parameters (Blocker)

These features are already available on SonarCloud, and will be included in SonarQube 8.3.

Enjoy!

Alex

5 Likes