Python

Use when: write or review idiomatic, typed, well-structured Python that is easy to test and maintain.

kimtth c478013 1.1 KB Updated

File contents

Goal: clear, typed Python that fails loudly and tests easily.

Use for:

  • new modules, scripts, or services in Python
  • reviewing code for idiom, typing, and structure
  • tightening loose, untyped, or side-effect-heavy code

Workflow:

  1. Add type hints at function and module boundaries.
  2. Prefer pure functions; isolate I/O and side effects.
  3. Use dataclasses or pydantic for structured data, not dicts.
  4. Handle errors with specific exceptions; avoid bare except.
  5. Manage resources with context managers (with-blocks).
  6. Verify with pytest, ruff/flake8, and a type checker (mypy/pyright).

Idioms:

  • comprehensions and generators over manual loops
  • pathlib over os.path string juggling
  • f-strings for formatting
  • enumerate/zip instead of index bookkeeping
  • prefer the standard library before adding a dependency

Rules:

  • type the boundaries; let inference handle locals
  • never catch Exception just to silence it
  • pin dependencies and use a virtualenv or uv
  • keep functions small and single-purpose; test behavior

kimtth/agent-skill-100-lines-or-less/tree/main/skills/python commit c478013e8c

Frequently asked questions

npx skillmds@latest add kimtth/python