This rule raises an issue when issue suppression comments have an incorrect format or syntax.

Why is this an issue?

Issue suppression comments like # NOSONAR and # noqa are essential tools for controlling code analysis. When these comments have incorrect syntax, they may not work as expected, leading to confusion about which issues are actually suppressed.

Python code analysis supports two main suppression formats: - # NOSONAR - SonarQube’s suppression comment - # noqa - Python’s standard "no quality assurance" comment

Each format has specific syntax rules. When these rules are violated, the suppression might fail silently or behave unexpectedly, making it unclear whether issues are intentionally ignored or accidentally unsuppressed.

What is the potential impact?

Incorrectly formatted suppression comments can lead to unintended code analysis behavior. Issues that developers think are suppressed might still be reported, while malformed syntax might cause the analyzer to ignore more issues than intended. This creates confusion during code review and reduces confidence in the analysis results.

How to fix it

Fix the syntax of issue suppression comments to follow the correct format.

For # NOSONAR: - Use # NOSONAR alone to suppress all issues on the line - Use # NOSONAR() with empty parentheses to suppress all issues - Use # NOSONAR(rule1, rule2) to suppress specific rules - Don’t use redundant commas in the parentheses, e.g. # NOSONAR(,) - Close all parentheses properly

For # noqa: - Use # noqa alone to suppress all issues on the line - Use # noqa: rule1,rule2 to suppress specific rules (with or without spaces after colon) - Don’t use redundant commas in the comma-separated lists, e.g. # noqa: ,rule1 - Don’t forget the colon (:) between noqa and the rule ID, and don’t use other punctuation

Code examples

Noncompliant code example

def example():
    x = 1  # NOSONAR(  # Noncompliant
    y = 2  # NOSONAR(a,)  # Noncompliant
    z = 3  # NOSONAR)(  # Noncompliant
    a = 4  # noqa: ,rule1  # Noncompliant
    b = 5  # noqa- rule1,rule2  # Noncompliant

Compliant solution

def example():
    x = 1  # NOSONAR
    y = 2  # NOSONAR(a)
    z = 3  # NOSONAR
    a = 4  # noqa: rule1
    b = 5  # noqa: rule1,rule2

Resources

Documentation