31

How can I ignore SonarQube warnings in Python code

In Java, I can use

@SuppressWarnings("squid:S1166")

Where the ID is the SonarQube rule ID. But what syntax should I use in Python?

I've tried

# noinspection python:S1313

but it didn't work.

To be clear, I'm looking for a solution in python code. NOT JAVA.

Daniel Scott
  • 7,418
  • 5
  • 39
  • 58
  • Possible duplicate of [In Sonar, how to prevent checking some rules in some packages?](http://stackoverflow.com/questions/6708628/in-sonar-how-to-prevent-checking-some-rules-in-some-packages) – CSchulz Jun 06 '16 at 12:10
  • 7
    That is quite clearly tagged as a java question... – Daniel Scott Jun 12 '16 at 07:46
  • I was also looking for an answer but then I realized: **code should not depend on the tools that are (will be) used to analyze it.** So, perhaps my (your) question was incorrect. This #NOSONAR thing (or anything more specific they can come up with) will work only for Sonar scanner, but not for any other linter. An issue with code is related to the code, not to an analyzer. – Pavel Smirnov Jan 06 '23 at 14:38
  • @PavelSmirnov Arguably this isn't code. These are code annotations directed at tools. So there's no problem with this question. – huyz Jan 07 '23 at 09:36
  • @huyz Perhaps I don't quite get you. I mean, there are hundreds of tools if not thousands. Should I add hundreds of annotations to the single line of my code? SQ may be the only or the main linter for Java, but it is not for Python. I understand why people add annotations for pylint, but for SQ, codacity, codeclimate et al... I am not so sure. P.S. Don't get me wrong, I voted the question up. – Pavel Smirnov Jan 23 '23 at 16:09
  • I agree that adding "control comments" is not a perfect solution, but it's better than many alternatives. .1 It keeps the code and hints for tools together, so that people (who may not have access to the tool) can see that the author knows about a potential problem. 2. The owner of the code is most likely the person who chooses which tool to use for scanning, avoiding having comments for many different tools. (Why is annotating for pylint different from SQ?) – Daniel Scott Jan 23 '23 at 16:20

2 Answers2

33

I believe the only syntax supported for Python (assuming it is supported) is the NOSONAR comment, so #NOSONAR or # NOSONAR at the end of the line where you want to ignore issues.

Unfortunately, this is a global issue suppression: it kills all issues on the line, not just those from a specific rule.

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
G. Ann - SonarSource Team
  • 22,346
  • 4
  • 40
  • 76
3

If you are using a sonar.properties file, you can set it up to ignore some specific rule on a given file or set of files.

Here is an example where you ignore different rules on differents files:

# Name your criteria
sonar.issue.ignore.multicriteria=e1,e2

# python:S3776 : Cognitive Complexity of functions should not be too high
sonar.issue.ignore.multicriteria.e1.ruleKey=python:S3776
sonar.issue.ignore.multicriteria.e1.resourceKey=src/my_project/complexe.py

# python:S117 : Local variable and function parameter names should comply with a naming convention
sonar.issue.ignore.multicriteria.e2.ruleKey=python:S117
sonar.issue.ignore.multicriteria.e2.resourceKey=src/my_project/**/views.py
Pierre
  • 2,552
  • 5
  • 26
  • 47
  • Unfortunately, this doesn't seem to be supported by [SonarLint](https://docs.sonarcloud.io/improving/sonarlint/) – Raphael Jun 30 '23 at 10:25