Sub topic 12
Privacy, accuracy, and maintenance risks
This chapter assumes you can run pytest, use Git and code review, and have used an AI chat or coding assistant. Earlier chapters showed how to generate and review tests and docstrings. Here you will look at what can go wrong around that work, and how to reduce each risk.
Three kinds of risk
Generated tests and documentation carry risks in three areas. Each needs a different habit.
| Risk | What goes wrong | Main defense |
|---|---|---|
| Privacy | Sensitive code or data leaves your control | Decide what you send before you send it |
| Accuracy | Output looks right but is wrong | Run it and compare it to the code |
| Maintenance | Output goes stale or becomes a burden | Treat it as code you own |
Privacy risks
When you paste code into an assistant, that code goes to a service you may not control. Check your organization's policy first. Some teams forbid sending proprietary code to external services. Others approve specific tools with specific data terms.
Things that should not appear in a prompt:
- Secrets such as API keys, passwords, and tokens.
- Real customer data, including names, emails, and account numbers.
- Code covered by a license or contract that bars sharing.
Test generation creates a particular trap. You may paste a real database row or a real API response so the assistant can "see the shape." Replace those values with invented ones first. The mock example from the earlier chapter used {"name": "Ada"} for this reason.
# Before sending: real data copied from a log
response = {"name": "Maria Lopez", "email": "[email protected]"}
# After redacting: invented values with the same shape
response = {"name": "Ada", "email": "[email protected]"}Also check what the assistant writes back. Generated tests sometimes embed literal values from your prompt. If any real value slipped through, it now sits in your test file and your Git history.
Accuracy risks
An assistant predicts plausible text. It does not run your code unless a tool lets it. So it can state something false in a confident tone. In tests and docs this shows up in a few ways:
- Wrong expected values. The assistant calculates by hand and gets it wrong, or copies the function's logic so the test cannot fail.
- Invented behavior. A docstring claims the function raises
TypeErrorwhen it never does. - Stale knowledge. The assistant describes a library API that changed in a newer version.
- Missing cases. Tests look thorough but skip an error path.
Take apply_discount from earlier. A docstring that says "Returns the price as an integer" would read fine and be false. The defense is mechanical: run the tests, run python -m doctest, and read each claim against the code.
def apply_discount(price: float, percent: float) -> float:
"""Return price reduced by percent, rounded to two decimals.
Raises ValueError if price is negative or percent is outside 0-100.
Examples:
>>> apply_discount(100, 10)
90.0
"""
if price < 0:
raise ValueError("price must be non-negative")
if not 0 <= percent <= 100:
raise ValueError("percent must be between 0 and 100")
return round(price * (1 - percent / 100), 2)Maintenance risks
Generated output is cheap to create, so it is easy to create too much. Each test and each docstring is something your team must keep correct.
- Volume. Hundreds of near-duplicate tests slow the suite and hide the ones that matter.
- Drift. A docstring or README describes the code on the day it was written. When the code changes, the text does not.
- Brittle tests. Tests that assert on internal calls break on every refactor even when behavior is unchanged.
- Unclear ownership. If nobody reads the output closely, nobody understands it later.
Reduce these by keeping what you can explain, deleting the rest, and putting checks in CI. Doctests and the test suite catch drift automatically. Reviewing generated changes in a pull request, like any other change, keeps a human accountable for them.
Exercise
A teammate pastes a production API response, including a real customer email, into an assistant to generate a mock for get_username. Name one privacy problem and one fix.
Show answer
The real email now leaves your control and may end up in the generated test file and Git history. Replace real values with invented ones, such as "[email protected]", before sending, and check the generated output for leftover real values.
Exercise
A generated docstring says a function raises TypeError on bad input, and the generated tests pass. Why is that not enough, and what check would catch the error?
Show answer
The tests may never exercise that path, so passing says nothing about the claim. Read the docstring against the code, add a test that triggers the claimed error, and run doctest or the suite to confirm the behavior exists.