PY
python-code-quality
Python code quality standards. Use when working with types, linting, type checking, exceptions, logging, or configuring ruff/ty/pyproject.toml.
Install
mkdir -p .claude/skills/python-code-quality && curl -L -o skill.zip "https://agentskills.codes/api/skills/download/16306" && unzip -o skill.zip -d .claude/skills/python-code-quality && rm skill.zipInstalls to .claude/skills/python-code-quality
Activation
This is the description your AI agent reads to decide when to run this skill — the better it matches your request, the more reliably it fires.
Python code quality standards. Use when working with types, linting, type checking, exceptions, logging, or configuring ruff/ty/pyproject.toml.143 chars✓ has a “when” trigger
About this skill
Python Code Quality
Typing Rules
- Must annotate all function signatures (parameters and return)
- Must type HTTP responses with Pydantic models—never return raw
dictfrom API endpoints - Must use generics (
list[str],dict[str, Any])—never barelist,dict,tuple - Must use Pydantic models at API boundaries (requests, responses, external data)
- Should use
T | NoneoverOptional[T] - Should use keyword-only args (
*) for functions with 3+ parameters - Should use
TypeAliasfor complex dict types instead of repeatingdict[str, Any] - Avoid
Anyexcept at true boundaries; prefer specific types or generics - Never use mutable defaults (
def f(items=[])); useNoneand initialize inside
Type Choice Guide
| Need | Use |
|---|---|
| API request/response | Pydantic BaseModel |
| DB + API unified | SQLModel |
| Internal data transfer | dataclass(frozen=True, slots=True) |
| Dict with known keys | TypedDict |
| Primitive wrapper | NewType |
| Fixed string values | Literal |
- Should use
NewTypefor domain primitives (UserId,OrderId) to prevent mixing - Avoid stringly-typed code—prefer enums or
Literalfor fixed values
Tooling
- Must use
rufffor linting and formatting - Must use
ty(astral.sh) for type checking - Must run before committing:
ruff format . && ruff check --fix . && ty check . - Must enable
ANN,TCH,I,E,F,Wruff rules - Should use
line-length = 120
See ../project-setup/references/pyproject.template.toml for complete configuration.
Exceptions
- Must define a base
ServiceErrorwithmessage,code,status_code - Must create specific exceptions inheriting from base (
NotFoundError,ConflictError) - Never swallow exceptions silently—always re-raise or wrap with context
- Should use
logger.exception()to include traceback before re-raising
Logging
- Must use structured logging (
structlogorloguruwith JSON sink in production) - Must include
request_idin all log entries - Never log sensitive data (tokens, passwords, PII)
Caching
- Should use
@lru_cachefor expensive pure functions - Must set
maxsizebased on expected input cardinality - Never cache functions with side effects or mutable state
- Should use
async-lru(@alru_cache) for async functions (not bare@lru_cache)