CQ004: MypyCheck

Overview

Property Value
ID CQ004
Name MypyCheck
Group code-quality
Severity WARNING

Description

Runs mypy type checker on the package and verifies no type errors are found.

mypy is a static type checker that can catch:

  • Type mismatches
  • Missing return statements
  • Incorrect function signatures
  • Incompatible assignments

What it checks

The check runs mypy on the package source and:

  • PASSED: mypy exits with code 0 (no errors)
  • FAILED: mypy finds type errors (reports count)
  • NOT_APPLICABLE: mypy is not installed or no py.typed marker

How to fix

Install mypy

pip install mypy
# or
uv add --dev mypy

Run mypy to see errors

mypy src/my_package
# or for the current directory
mypy .

Add type annotations

Fix errors by adding proper type hints:

# Before
def greet(name):
    return f"Hello, {name}!"

# After
def greet(name: str) -> str:
    return f"Hello, {name}!"

Configure mypy

Add to pyproject.toml:

[tool.mypy]
python_version = "3.11"
strict = true
warn_return_any = true
warn_unused_configs = true

[[tool.mypy.overrides]]
module = "tests.*"
disallow_untyped_defs = false

Common fixes

Missing imports:

pip install types-requests  # For requests library
pip install pandas-stubs    # For pandas

Ignore specific errors:

result = some_dynamic_call()  # type: ignore[no-untyped-call]

Handle Optional types:

from typing import Optional

def get_user(user_id: int) -> Optional[User]:
    ...

Create py.typed marker

If not present, create py.typed to indicate typed package:

touch src/my_package/py.typed

Why WARNING severity?

This check is a WARNING because:

  • Type checking is optional but highly recommended
  • Some codebases have legacy untyped code
  • Third-party libraries may lack type stubs

Configuration

Skip this check

[tool.pycmdcheck]
skip = ["CQ004"]

CLI

pycmdcheck --skip CQ004