Match mypy Error Codes to Actual Error Type

lesson tools active tools/match-mypy-error-codes-to-actu.md View on GitHub

Match mypy Error Codes to Actual Error Type

Rule

Match mypy error codes to the actual error type when using type: ignore comments. Use import-untyped for packages missing type stubs, import-not-found for missing packages. Mismatched codes cause mypy to report both 'Unused type: ignore comment' and the original error.

Context

Mypy requires specific error codes in type: ignore comments to suppress warnings. Using the wrong code (e.g., import-not-found when the package exists but lacks stubs) results in the comment being ignored, producing dual errors. The better solution is often to fix the root cause: install missing packages or add type stub dependencies.

Detection

Pattern

Common error codes:

# ❌ Wrong: Using import-not-found for installed package without stubs
import requests  # type: ignore[import-not-found]

# ✅ Correct: Using import-untyped for package without stubs
import requests  # type: ignore[import-untyped]

# ✅ Better: Add type stubs to dependencies
# In pyproject.toml: "types-requests"

Outcome

Related

Match Keywords

type: ignore mypy import error import-not-found import-untyped Cannot find implementation or library stub