Python docstrings are not considered as comment

I’m facing false positives in Python, concerning the 20% comment/code ratio rule.
I’m using Sonarqube Community Edition (Version 8.9.6 build 50800).

According to PEP258, docstring can have the following formats

"""Module docstring -> comment"""

g = 'module attribute (module-global variable)'
"""This is g's docstring. -> Not a comment"""

class AClass:

    c = 'class attribute'
    """This is AClass.c's docstring. -> Not a comment"""

    def __init__(self):
        """Method __init__'s docstring.-> comment"""

        self.i = 'instance attribute'
        """This is self.i's docstring. -> not a comment"""

def f(x):
    """Function f's docstring. -> comment"""
    return x**2

f.a = 1
"""Function attribute f.a's docstring. -> Not a comment"""

Yet, some of them only are considered as comments in Sonarqube. When it’s not, this is considered as code and my ratio becomes high!

Is it normal ? Desired ?
Thanks for your help!

up this thread!

Hey there.

As stated in our rules, please be patient.

Since all the necessary information has been included, we’ve flagged this for attention by an expert. This means that somebody will look at your report, maybe ask some follow-up questions, and try and determine if it’s really a false-positive that should be fixed.

This review could be done hours, days, or even weeks from now. If it takes a while – it doesn’t mean your report isn’t important to us, it just means that our teams are already hard-at-work developing new language analysis features, and your report is in the queue.

Hello @emonier,

First, sorry for the late answer.
Indeed, the behavior is inconsistent and it seems that attribute docstrings are not considered as proper docstring by the analyzer, and therefore not recognized as comments.

I created the following ticket to fix this.

Cheers,
Guillaume