Python Language
Priority: P0 (CRITICAL)
Rules
- Prefer
from __future__ import annotationsin modern modules. - Type public functions, return values, and boundary data shapes.
- Use
pathlib.Path,contextlib,enum, and stdlib helpers before custom wrappers. - Use
@dataclass(frozen=True)for immutable contracts and value objects. - Prefer
Protocol,TypedDict, or small dataclasses overdict[str, Any]sprawl.
Recipe
- Model contracts explicitly with dataclasses, typed dicts, or pydantic at I/O boundaries.
- Keep imports stable: stdlib, third-party, local.
- Use comprehensions and generators carefully; stop when readability drops.
- Choose explicit timezone-aware datetimes for persisted or user-visible timestamps.
- Keep module side effects minimal; import should not start work.
Anti-Patterns
- No mutable defaults: use
field(default_factory=...). - No
os.pathin new code: preferPath. - No
Anyas escape hatch: narrow unknown shapes or validate them. - No import-time env reads for volatile runtime state unless cached config is intentional.
Verify
- Run parser/type checks after cross-module type changes.
- Load Framework Map for category defaults.