This skill helps you implement precise type hints and Pydantic models to fix type errors and improve code clarity.
npx playbooks add skill gigaverse-app/skillet --skill pythonista-typing
Use Pydantic models for structured data. Use specific types everywhere. Never use Any or raw dicts when structure is known.
# 1. Run type checker
pyright <file-or-directory>
# or
basedpyright <file-or-directory>
# 2. Fix errors (see patterns below and reference files)
# 3. Verify no behavior changes
pytest tests/
cast() for runtime-compatible but statically unverifiable types# type: ignore only for legitimate cases# CORRECT
def process_data(items: list[str]) -> dict[str, list[int]]:
results: dict[str, list[int]] = {}
return results
# WRONG - Old style
from typing import Dict, List
def process_data(items: List[str]) -> Dict[str, List[int]]:
...
# NEVER
items: list[Any]
data: dict
result: Any
# ALWAYS use specific types
items: list[DataItem]
data: dict[str, ProcessedResult]
result: SpecificType | OtherType
# WRONG - Type cop-out
def process(obj: Any):
if hasattr(obj, "name"):
return obj.name
# CORRECT - Use Protocol
class Named(Protocol):
name: str
def process(obj: Named) -> str:
return obj.name
# WRONG - Unreadable
def execute() -> tuple[BatchResults, dict[str, Optional[Egress]]]:
pass
# CORRECT - Named model
class ExecutionResult(BaseModel):
batch_results: BatchResults
egress_statuses: dict[str, Optional[Egress]]
def execute() -> ExecutionResult:
pass
Rule: If you can't read the type annotation out loud in one breath, it needs a named model.
# WRONG - Raw dict
def get_result() -> dict[str, Any]:
return {"is_valid": True, "score": 0.95}
# CORRECT - Pydantic model
class Result(BaseModel):
is_valid: bool
score: float
def get_result() -> Result:
return Result(is_valid=True, score=0.95)
NEVER use TypedDict or dataclasses without explicit authorization. Always use Pydantic.
# WRONG - Breaking type safety
result_dict = result.model_dump()
result_dict["_run_id"] = run_id # Now untyped!
# CORRECT - Extend the model
class ResultWithRunId(BaseModel):
details: ResultDetails
run_id: str | None = None
from typing import cast
# Preferred
typed_results = cast(list[ResultProtocol], results)
selected = select_by_score(typed_results)
# Less clear
selected = select_by_score(results) # type: ignore[arg-type]
Only for:
func._attr = val # type: ignore[attr-defined]DO NOT use for simple fixes (add Optional, fix return types).
For detailed patterns:
Weekly roundup of top Claude Code skills, MCP servers, and AI coding tips.