首页 > 解决方案 > Sonarqube 如何使用 # NOSONAR 在 Python 中抑制特定警告

问题描述

# NOSONAR在 Python 中,我们可以通过应用注释来抑制代码中特定行的所有 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

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

标签: pythonsonarqube

解决方案


如果您使用的是 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是您要抑制此特定规则的文件。不幸的是,它会抑制整个文件,而不是特定功能。


推荐阅读