6 New Rules to Support Python Type Hints

Hello Python users,

We have a new set of rules in our Python analysis to ensure the use of type hints result in expected behaviors.

These are the new rules:

  • S6538: Function returns should have type hints
  • S6540: Function parameters should have type hints
  • S6542: “Any” should not be used as a type hint
  • S6543: Type hints of generic types should specify their type parameters
  • S6545: Built-in generic types should be preferred over the typing module in type hints
  • S6546: Union type expressions should be preferred over “typing.Union” in type hints

As type hinting is optional, these rules will need to be activated for those who use Python type hinting.
These rules are available now on SonarCloud and will be available on SonarQube 10.1.

Your feedback is appreciated.




For Python static code analysis we enabled this on our project:

However, I would like to disable this rule for Unit/Integration tests where a Function (In most scenarios) is None. I would prefer to not require adding it to every test.

For functions where we are actually returning something (ie: A yield or return keywords) I do want to enforce the rule.

Hi @Bryan_Fauble,

Thank you very much for your message, I am terribly sorry for the late reply.

Currently, it is not possible to disable this rule for test functions that do not have a yield or return keyword, but by default, this rule should not trigger on test code. I do not have access to the project you provided, but I would suggest verifying that the project’s configuration correctly defines the tests folder (the sonar.tests key in the sonar-project.properties file).

I know this is not ideal, but this could be a workaround where only production code would raise such issues. Would that be a potential solution for you?
Otherwise, we could consider having this behavior on production code as well (not raising issues on functions with return statements), which could be enabled through a rule parameter.

Sorry again for the delayed response.

Kind regards,