Loading...
Loading...
This skill should be used when the user asks to "add mypy to a project", "set up static type checking", "fix mypy errors", "configure mypy", "annotate Python code with types", or needs guidance on Python type checking best practices with mypy.
npx skill4agent add the-perfect-developer/the-perfect-opencode python-mypypip install mypy
# Install type stubs for common third-party libraries
pip install types-requests types-PyYAML types-boto3
# Run mypy
mypy src/
mypy --strict src/from collections.abc import Iterable, Sequence
def process(items: list[str], limit: int = 10) -> list[str]:
return items[:limit]
# Use abstract types for parameters (accept more, restrict less)
def summarize(records: Iterable[dict[str, str]]) -> str:
return ", ".join(r["name"] for r in records)
# None return type for procedures
def log_event(event: str, level: str = "INFO") -> None:
print(f"[{level}] {event}")# Python 3.10+ syntax (preferred)
def find_user(user_id: int) -> str | None:
...
# Python 3.9 and earlier
from typing import Optional, Union
def find_user(user_id: int) -> Optional[str]:
...
# Union for multiple possible types
def normalize_id(user_id: int | str) -> str:
if isinstance(user_id, int):
return f"user-{user_id}"
return user_idfrom typing import ClassVar
class Repository:
default_limit: ClassVar[int] = 100
def __init__(self, name: str, url: str) -> None:
self.name = name
self.url = url
self._cache: dict[str, list[str]] = {}
def fetch(self, query: str) -> list[str]:
return self._cache.get(query, [])TypedDictdict[str, Any]from typing import TypedDict
class UserRecord(TypedDict):
id: int
name: str
email: str
class PartialUser(TypedDict, total=False):
nickname: str
avatar_url: str
def create_user(data: UserRecord) -> None:
print(data["name"]) # mypy knows this is strProtocolfrom typing import Protocol
class Closeable(Protocol):
def close(self) -> None: ...
class Readable(Protocol):
def read(self, n: int = -1) -> bytes: ...
def process_stream(stream: Readable) -> bytes:
return stream.read()pyproject.toml[tool.mypy]
python_version = "3.12"
warn_return_any = true
warn_unused_configs = true
warn_unused_ignores = true[tool.mypy]
python_version = "3.12"
warn_return_any = true
warn_unused_configs = true
warn_unused_ignores = true
disallow_incomplete_defs = true
check_untyped_defs = true[tool.mypy]
python_version = "3.12"
strict = true
warn_unused_ignores = truestrictdisallow_untyped_defsdisallow_any_genericswarn_return_anyno_implicit_reexportstrict_equality# Check if stubs are available
mypy --install-types
# Install specific stubs
pip install types-requests types-PyYAML types-redisignore_missing_imports# pyproject.toml — preferred approach
[[tool.mypy.overrides]]
module = ["boto3.*", "botocore.*", "some_untyped_lib"]
ignore_missing_imports = trueimport untyped_lib # type: ignore[import-untyped]
result = complex_dynamic_call() # type: ignore[no-any-return] # reason: third-party returns Anymypy --ignore-missing-imports src/check_untyped_defs = truedisallow_incomplete_defsdisallow_untyped_defs[[tool.mypy.overrides]]strict = true# Migrate package-by-package
[[tool.mypy.overrides]]
module = "myapp.core.*"
disallow_untyped_defs = true
[[tool.mypy.overrides]]
module = "myapp.legacy.*"
ignore_errors = true # deal with laterfrom pydantic import BaseModel
class Item(BaseModel):
name: str
price: float
tags: list[str] = []
# FastAPI + mypy works seamlessly — Pydantic models are fully typed
from fastapi import FastAPI
app = FastAPI()
@app.get("/items/{item_id}")
async def get_item(item_id: int) -> Item:
return Item(name="Widget", price=9.99)[tool.mypy]
plugins = ["sqlalchemy.ext.mypy.plugin"]from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
class Base(DeclarativeBase):
pass
class User(Base):
__tablename__ = "users"
id: Mapped[int] = mapped_column(primary_key=True)
name: Mapped[str]
email: Mapped[str | None]isinstanceassertdef render(value: str | int | None) -> str:
if value is None:
return ""
if isinstance(value, int):
return str(value)
return value.upper() # mypy knows: value is str herereveal_type()x = [1, 2, 3]
reveal_type(x) # Revealed type is "builtins.list[builtins.int]"# .pre-commit-config.yaml
repos:
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.9.0
hooks:
- id: mypy
additional_dependencies:
- types-requests
- pydantic- name: Run mypy
run: mypy src/ --config-file pyproject.toml| Pattern | Syntax |
|---|---|
| Basic function | |
| Optional param | |
| List of strings | |
| Dict | |
| Callable | |
| Any iterable | |
| Typed dict | |
| Protocol | |
| Suppress line | |
| Debug type | |
| Forward ref | |
| Flag | Purpose |
|---|---|
| Enable all optional checks |
| Require all function annotations |
| Check bodies of unannotated functions |
| Warn on implicit |
| Catch stale |
| Suppress missing stub errors |
references/configuration-guide.mdpyproject.tomlreferences/type-patterns.mdexamples/pyproject.toml