Course contentsShow
← Python
Lesson 593 of 1,0609. Type Safety and Code QualityFree lesson

Handling Legacy Codebases with Incremental Linting

Apply selective linting rules to legacy files while enforcing strict quality standards on all new code contributions to the repository.

In this lesson, you will learn how to maintain high quality in your Python projects by enforcing strict linting on new code while applying more relaxed, selective rules to your legacy codebase.

When working with a massive, older repository, enabling strict settings like mypy --strict or full Ruff rule sets globally often results in thousands of errors. This "error fatigue" can paralyze development. The solution is incremental linting, which allows you to draw a line in the sand.

The Strategy: Selective Enforcement

You can achieve this by configuring your tools to ignore legacy directories while holding new contributions to higher standards.

For example, in your pyproject.toml, you can use mypy configuration sections to handle legacy modules differently:

# Apply strict rules to new modules
[tool.mypy]
strict = true

# Override for the legacy directory
[[tool.mypy.overrides]]
module = "legacy_system.*"
ignore_errors = true

By setting ignore_errors = true for your legacy folder, you prevent current developers from being overwhelmed by historical technical debt. As you touch legacy files for bug fixes or refactoring, you can remove them from this "ignore list" file-by-file or package-by-package.

Integrating with Pre-commit Hooks

To ensure this works in practice, use your pre-commit hooks to only run checks on files that have changed in a commit (staged files). By using a tool like ruff or mypy configured to respect your pyproject.toml ignore rules, you ensure that any new code added to the codebase—even if it resides in a legacy folder—is checked against the strict rules, provided you haven't explicitly excluded that specific file path.

This creates a "ratchet effect": the quality of the codebase only moves in one direction. You aren't required to fix the entire legacy system at once, but you ensure that no new "bad" code is allowed to enter the ecosystem.

Key Takeaway: Use configuration overrides to exempt legacy directories from strict linting, ensuring you can enforce high standards on new code without being blocked by historical debt.