We can't find the internet
Attempting to reconnect
Something went wrong!
Attempting to reconnect
← Modules, Packaging and Tooling step 36 of 41
Clean Up Old noqa Comments
Your project has collected # noqa comments over two years. A # noqa
comment at the end of a line tells Ruff to hide findings on that line.
# noqa: T201 hides only rule T201. A bare # noqa, with no codes, hides
findings of every rule. When someone fixes the code and leaves the comment
behind, it hides nothing today. It will hide the next finding of that rule
on that line.
To see what each comment hides, run Ruff with --ignore-noqa, which ignores
the comments. --quiet drops the summary at the end, so every line of
output is a finding:
ruff check --ignore-noqa --output-format concise --quiet app.py
Each output line gives the file, the line, the column, the rule code and a message:
app.py:3:1: T201 `print` found
Write code that works out which codes each comment still needs:
source = "import os # noqa: F401\n\nprint(os.getcwd()) # noqa: T201, E722\n"
needed_codes(source, ["app.py:3:1: T201 `print` found"])
# {1: [], 3: ["T201"]}
The code uses os, so the comment on line 1 hides nothing. On line 3, Ruff
found only T201, so E722 can go.
What to write
Fill in parse_finding and needed_codes.
parse_finding(output_line)returns aFindingwith the line number and the rule code. An output line looks likepath:line:column: CODE message. Paths contain no colons.needed_codes(source, output)returns a dict with one entry for each line ofsourcethat contains# noqa. Lines count from 1.- A colon and one or more codes can follow
# noqa. Commas, spaces or both separate the codes. The first code can come right after the colon. - Text after a second
#is a note. Ignore it. - For a comment that lists codes, keep each code that a finding on the same line has. Keep them in the comment's order.
- For a bare
# noqa, list the codes of the findings on its line, sorted, each once. Listing the codes lets Ruff still report every other rule on that line.
Edge cases
- An empty list means the comment hides nothing.
- One line can have the same code twice, such as two
printcalls. List it once. outputcan be empty, and so cansource.
What the driver reports
Keep solve as it is. It returns comments, one string per # noqa
comment, in line order. "line 3: # noqa: T201" shows how the comment
should read, and "line 1: delete" means it hides nothing.
Going further
Ruff can run this check for you. Rule RUF100 reports codes that hide
nothing. Select it, and ruff check --fix removes them. Rule PGH004
reports every bare # noqa.
Your code, saved here
Sign in to access your accepted solutions and saved draft history. You can keep reading the lesson without an account.
Sign in to your accountStuck?
Python reference solution
Sign in to track your attempts toward unlocking the reference solution.