Sonarqube 如何使用 # NOSONAR 抑制 Python 中的特定警告

Sonarqube How to supress a specific warning in Python using # NOSONAR

在 Python 中,我们可以通过应用 # NOSONAR 注释来抑制代码中特定行的所有 Sonarqube 警告。这并不理想。有没有办法抑制特定错误,而不是抑制所有错误?

例如,您可能有一个带有两个警告的函数:

Function "foo" has 8 parameters, which is greater than the 7 authorized.
Refactor this function to reduce its Cognitive Complexity from 17 to the 15 allowed

你怎么能压制第一个,而不压制第二个呢?

如果您使用的是 Jenkins,这是一种解决方法,但并不完美。它可用于抑制整个文件的特定警告(而不仅仅是一个函数)。

在 Jenkins 属性 文件中添加如下内容:

sonar.issue.ignore.multicriteria=e1
sonar.issue.ignore.multicriteria.e1.ruleKey=python:S107
sonar.issue.ignore.multicriteria.e1.resourceKey=path/to/file.py

其中 python:S107 是具有 7 个以上参数的函数的规则键,path/to/file.py 是您要为其禁止此特定规则的文件。不幸的是,它会在整个文件中抑制它,而不是特定的函数。