Calmcode - ruff: ignore

Ignoring Checks

1 2 3 4 5

Let's consider the following Ruff configuration. It mainly just points to the flake8-print rules.

[lint]
select = ["T20"]
ignore = ["D211", "D213"]

This configuration will check for print statements. So that means that it will complain when it sees this Python script:

def fibonachi(s1, s2, n):
    result = [s1 , s2]
    for i in range(n):
        s1, s2 = s2, s1 + s2
        result.append(s2)
    return result 

if __name__ == "__main__":
    print(fibonachi(1, 1, 10))

There's a print statement at the end of the file. But lets consider the same file but with another print statement.

def fibonachi(s1, s2, n):
    print("this one over here")
    result = [s1 , s2]
    for i in range(n):
        s1, s2 = s2, s1 + s2
        result.append(s2)
    return result 

if __name__ == "__main__":
    print(fibonachi(1, 1, 10))

In this case I might argue that I want to get rid of the first print statement, which was probably left as an artifact of a debugging exercise. But the second print statement feels like it might be intended behabior. So lets configure Ruff to ignore that last one.

Ignoring

To deal with these situations, we can tell ruff to ignore certain lines by adding a # noqa comment.

def fibonachi(s1, s2, n):
    print("this one over here")
    result = [s1 , s2]
    for i in range(n):
        s1, s2 = s2, s1 + s2
        result.append(s2)
    return result 

if __name__ == "__main__":
    print(fibonachi(1, 1, 10))  # noqa: T201

We could just put a # noqa comment on that final line but that would ignore all the rules. It's better to be specific and to explicitly type the rule that you're interested in ignoring.

When you now run ruff and allow it to make the unsafe fixes, it will remove the first print statement but not the second one.