【问题标题】:Sonarqube How to supress a specific warning in Python using # NOSONARSonarqube 如何使用 # NOSONAR 在 Python 中抑制特定警告
【发布时间】:2020-05-13 05:36:02
【问题描述】:

在 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

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

【问题讨论】:

  • 如果我的问题没有错...您的意思是要使用“@SuppressWarnings("squid:S###")”而不是 NOSONAR?
  • @Ehsan @SupressWarnings 在 Python 中可用吗?我认为这仅在 Java 中是允许的。
  • 这能回答你的问题吗? Ignore SonarQube warnings in python
  • @agabrys 不,这个答案说使用 #NOSONAR “是一个全球性的问题抑制:它会杀死在线上的所有问题,而不仅仅是来自特定规则的问题。”我正在寻找一种机制来抑制一个特定规则,而不是所有规则。

标签: python sonarqube


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-09
    • 2010-10-03
    • 2010-11-04
    • 1970-01-01
    • 2015-04-13
    • 1970-01-01
    • 2021-10-09
    • 1970-01-01
    相关资源
    最近更新 更多