Python Type Hints and mypy Strict Retrofit Prompt
Incrementally add type hints to an untyped Python automation script and get it passing mypy --strict — annotating signatures, taming Any, modeling Optional, and wiring a CI gate — without a risky big-bang rewrite.
- Target user
- Python developers hardening legacy automation scripts
- Difficulty
- Intermediate
- Tools
- Claude, ChatGPT
The prompt
You are a senior Python engineer who retrofits type hints into crusty automation scripts and gets them to `mypy --strict` clean — incrementally, so the codebase stays green the whole way. I will provide: - The untyped module(s) and roughly how big - The Python version (so you pick the right syntax: `X | None` vs `Optional[X]`, `list[str]` vs `List[str]`) - Whether there's already a CI pipeline and any mypy config Your job: 1. **Set the baseline** — give me a `mypy` config in `pyproject.toml` that starts permissive (module-level `ignore_errors` or `--no-strict-optional` off only where needed) so CI goes green today, then ratchets toward `strict = true`. Strictness should only increase. 2. **Annotate signatures first** — add parameter and return types to every public function/method; these give the most checking value per edit. Use the modern syntax for my Python version. Don't annotate every local variable — let inference work. 3. **Tame `Any` and untyped libraries** — find implicit `Any` (`--disallow-untyped-defs`, `warn_return_any`); for third-party libs without stubs, install `types-*` packages or add a narrowly-scoped `[[tool.mypy.overrides]]` with `ignore_missing_imports`, not a blanket ignore. 4. **Model Optional honestly** — replace functions that secretly return `None` on failure with `Optional[T]` (`T | None`) and make callers handle it; this is where mypy finds real latent bugs. Highlight each None-related fix as a likely bug, not just a type chore. 5. **Precise containers and unions** — `dict[str, int]` not bare `dict`; `Sequence`/`Mapping` for read-only params; `TypedDict` or a `@dataclass` for dict-shaped records; `Literal`/`Enum` for fixed string sets. 6. **Escape hatches, used sparingly** — `cast()`, `# type: ignore[code]` with the specific error code and a comment, and `typing.assert_never` for exhaustiveness. Never a bare `# type: ignore`. 7. **CI gate** — add a `mypy` step that fails the build, and a plan to flip on each strict flag module-by-module so the ratchet is enforced. 8. **Verify** — run mypy and show it clean; call out any genuine bug the types surfaced. Output: (a) the ratcheting `pyproject.toml` mypy config, (b) the annotated module, (c) a list of real bugs the typing exposed, (d) the CI step. Bias toward incremental green-the-whole-way over a big-bang rewrite, and treat Optional fixes as bug hunts.
Run this prompt with AI
Test it, get an AI-improved version, or compare models — live in the Prompt Workspace. No copy-paste.
Related prompts
-
Python pathlib Filesystem Refactor Prompt
Refactor brittle os.path and string-concatenated file handling into clean, cross-platform pathlib code — safe joins, globbing, existence checks, and atomic-friendly path operations.
-
Python Typer CLI App Scaffold Prompt
Generate a modern, type-hint-driven CLI using Typer — subcommands, validated options, rich help, shell completion, and clean exit codes — for operations tools that need to feel like first-class CLIs.
-
Migrate Bash to Python Prompt
Convert an overgrown, hard-to-maintain Bash script into clean, testable Python — preserving behavior while replacing fragile string-munging, brittle error handling, and unquoted expansions with structured, idiomatic code.
-
Bash Nameref (declare -n) Indirect-Variable Script Prompt
Write a Bash script that uses namerefs (declare -n) to pass arrays and associative arrays into functions by reference and return structured results, replacing brittle eval-based indirection.
More Bash & Python Automation prompts & error guides
Browse every Bash & Python Automation prompt and troubleshooting guide in one place.
Reading prompts? Get all 500 in one free PDF
500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.
- 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
- Instant PDF download — yours free, forever
- Plus one practical AI-workflow email a week (no spam)
Single opt-in · unsubscribe anytime · no spam.