Developer tools · · 4 min read
Ruff: catch two mistakes in AI-written Python before handoff
Run a focused lint check on a Python brief-summary helper, inspect undefined and unused names, then test what a clean lint result still misses.
By Sociologix Editorial

Check the helper before connecting it to an AI worker
This AI-assisted editorial guide uses official Ruff documentation checked December 3, 2026. The examples below were run locally with Ruff 0.17.6, whose release was verified on PyPI. The deliberately flawed helper is an editorial example, not the output of a measured model evaluation.
AI-built automation often needs small Python helpers: count tasks, prepare a summary or shape a record for review. A helper can look plausible while referring to a variable that was never created. Catching that mistake before connecting a script to files, clients or a model keeps the first debugging step small.
This lab uses fictional website-scope data. It checks two rules, repairs one expression and then demonstrates a data problem that those rules cannot detect. There are no model calls, customer messages or production changes in the lab.
1. Pin the tool in a separate environment
Ruff is a Python linter and formatter available through PyPI. Its official installation guide supports pip; use a dedicated environment rather than changing an existing application’s dependencies. [1] These commands are for Windows PowerShell in an empty exercise folder. They install the version used for the observations here.
Keep the version in your team’s development setup when adopting the check. The goal is a repeatable result across an AI worker’s editing environment and the person reviewing its changes, rather than a moving set of diagnostics. Installing Ruff does not connect it to your model provider.
python -m venv .venv
.\.venv\Scripts\python.exe -m pip install "ruff==0.17.6"
.\.venv\Scripts\ruff.exe --version2. Start with one intentionally broken file
Save this as brief_summary_bad.py. It reads a goal and task list but refers to task_count in the result without defining it. The tasks variable is assigned and then ignored. Ruff’s F821 rule checks undefined names; F841 identifies unused variables within functions. [2][3]
These findings point to a review question: was the list supposed to be counted? Do not hide the warning by renaming tasks to _tasks. That would suppress a symptom while leaving the missing count unresolved. Likewise, inventing a fixed number would make the summary misleading.
def summarize(brief):
goal = brief["goal"]
tasks = brief["tasks"]
return f"{goal}: {task_count} tasks"
3. Run a narrow check and inspect the result
Run the command below against that file. --select limits the exercise to the two named rules, while --isolated ignores configuration files so an unrelated parent configuration does not change the lab. These options are documented in Ruff’s configuration and CLI reference. [4]
The local run reported F841 for tasks and F821 for task_count, with exit code 1. This is a lint result, not a failed installation. Ruff documents code 0 for a clean check, 1 for detected violations and 2 for abnormal termination such as invalid configuration or command options. [5] This exercise does not use --fix or --exit-zero.
.\.venv\Scripts\ruff.exe check --isolated --select F821,F841 --no-cache brief_summary_bad.py4. Repair the expression and check its behavior
Save the corrected version below as brief_summary.py. It derives the count from tasks instead of inventing a new variable. Re-run the same lint command with the corrected file name. In the local exercise, that file returned exit code 0 and no diagnostics for the selected rules.
Then import summarize in a Python test or interpreter. With goal set to Website refresh and tasks set to the list ["Home", "Contact"], the observed result was “Website refresh: 2 tasks.” An empty list should produce a count of zero. Compare actual text, not just whether Python finished without an exception.
def summarize(brief):
goal = brief["goal"]
tasks = brief["tasks"]
return f"{goal}: {len(tasks)} tasks"
assert summarize({"goal": "Website refresh", "tasks": ["Home", "Contact"]}) == "Website refresh: 2 tasks"
assert summarize({"goal": "Website refresh", "tasks": []}) == "Website refresh: 0 tasks"
5. Record what a clean check does not establish
A clean result under two rules does not mean the helper is ready for arbitrary model output. In an additional local case, tasks was the string "Home" instead of a list. The corrected function returned “Website refresh: 4 tasks,” because len counted characters. Neither selected rule is a schema check.
Before using this pattern in an AI workflow, validate the input structure and define whether empty task lists are allowed. Test missing keys, wrong containers and unexpected task values against that contract. The summary also does not decide whether a task is within an agreed scope or whether a delivery date is realistic.
Give the AI coding worker the diagnostic and the intended behavior together: count the supplied task list; keep the goal text; add a test with two tasks and one with zero. Inspect its diff before accepting the repair. If an unused value may have side effects, assess the assignment before deleting it: Ruff specifically marks the F841 automatic fix as unsafe because attached comments may be removed. [3]
- Keep the lint command and version beside the reviewed change.
- Treat diagnostic suppression as a decision to explain, not a default fix.
- Run behavioral and input-contract tests separately from lint.
- Keep approval and customer-facing delivery outside this local code-quality check.
Sources & further reading
Give AI-built automation a reviewable handoff
Sociologix can help design automation workflows with input validation, reproducible checks and a clear review step before code or generated work reaches customers.
Talk to Sociologix