A comment holds code that was switched off
What it finds
Code left in comments is not run, not tested and not updated when the code around it changes; version control keeps old code better. A comment counts as code when its text parses as a Python statement and looks like one: it contains =, a call, return, import, def, class or a block keyword followed by its colon. Comments that read as prose, pragmas such as type: and noqa, and shebangs are left alone. A run of comment lines is reported once, at its first line of code.
From the rule's source, disabled_code_comment.rs.
def total(items): # result = sum(items)tx0 reports: this comment holds code that does not run # return result # The total is computed below. # TODO: handle negative values # if x is None: this is prose. count = len(items) # print(items)tx0 reports: this comment holds code that does not run return sum(i for i in items) + count # type: ignore Turn it on, off or suppress it
The rule runs from the strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/python:disabled-code-comment" = "note"To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:
# tx0: ignore[tx0/python:disabled-code-comment] reason="..."tx0 explain tx0/python:disabled-code-comment shows the same in the terminal. Found a false positive? Report it.