.clauderules 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # Claude Code Rules - Follow Every Rule Exactly
  2. You must prioritize straightforward code semantics, well-named types, clear function signatures, and robust, carefully-chosen abstractions. Think about how your decisions might impact these aspects of code quality before proposing any changes.
  3. You have access to all modern Python features from Python 3.13, 3.12, 3.11...
  4. **When you're done making changes, remove any redundant comments; remaining comments should only apply to complex code segments, adding relevant context.**
  5. ## 1. Code Discipline
  6. * Eliminate superfluous `try`/`catch` and `if` branches through strict typing and static analysis.
  7. * Use pure functions unless you must mutate fixed state—then wrap that state in a class.
  8. * Every function is **referentially transparent**: same inputs ⇒ same outputs, no hidden state, no unintended I/O.
  9. * Put side-effects in injectable "effect handlers"; keep core logic pure.
  10. ## 2. Naming
  11. * Choose descriptive, non-abbreviated names—no 3-letter acronyms or non-standard contractions.
  12. * Anyone reading a function's type signature alone should grasp its purpose without extra context.
  13. ## 3. Typing
  14. * Maintain **strict, exhaustive** typing; never bypass the type-checker.
  15. * Default to `Literal[...]` when an enum-like set is needed.
  16. * Prefer built-in types; when two values share structure but differ in meaning, enforce separation:
  17. * Use `typing.NewType` for primitives (zero runtime cost).
  18. * For serializable objects, add a `type: str` field that states the object's identity.
  19. ## 4. Pydantic
  20. * Read, respect, and rely on Pydantic documentation.
  21. * Centralize a common `ConfigDict` with `frozen=True` and `strict=True` (or stricter) and reuse it everywhere.
  22. * For hierarchies of `BaseModel` variants, declare a discriminated union with `typing.Annotated[Base, Field(discriminator='variant')]`; publish a single `TypeAdapter[Base]` so all variants share one strict validator.
  23. ## 5. IDs & UUIDs
  24. * Subclass Pydantic's `UUID4` for custom ID types.
  25. * Generate fresh IDs with `uuid.uuid4()`.
  26. * Create idempotency keys by hashing *persisted* state plus a **function-specific salt** to avoid collisions after crashes.
  27. ## 6. Error Handling
  28. * Catch an exception **only** where you can handle or transform it meaningfully.
  29. * State in the docstring **where** each exception is expected to be handled and **why**.
  30. ## 7. Dependencies
  31. * Introduce new external dependencies only after approval.
  32. * Request only libraries common in production environments.
  33. ## 8. Use of `@final` & Freezing
  34. * Mark classes, methods, and variables as `@final` or otherwise immutable wherever applicable.
  35. ## 9. Repository Workflow
  36. If you spot a rule violation within code that you've not been asked to work on directly, inform the user rather than patching it ad-hoc.
  37. ---
  38. ### One-Sentence Summary
  39. Write strictly-typed, pure, self-describing Python that uses Pydantic, well-scoped side-effects, immutable state, approved dependencies, and explicit error handling.