agentleFS
nledfordnledford/engineering-review-board/skills/python-design-patterns/SKILL.md

python-design-patterns

Skill11 starsChanged 8 days agoMITView on GitHub

Python design pattern guidance. Use when choosing, implementing, or reviewing idiomatic Python patterns such as dataclasses, value objects, protocols, context managers, dependency injection, adapters, repositories, factories, CLI/service boundaries, async resource handling, pytest fixtures, or Clean/Hexagonal/Onion expression in Python. Use python-antipatterns for smell-focused audits and python-engineering for broader Python workflow.

15 other projects use a version of this.

Adopted by

Most recently taken up by

Used with

Projects with it also use python-type-safety, python-error-handling and 7 more.

Other files people use alongside this one.

Files found in the same public projects as a version of this one, more often than they turn up anywhere else. Each project counts once, large collections count for less, and this file's own repository is left out.
  • CodeWithBehnamSkill

    python-type-safety

    Python type safety with type hints, generics, protocols, and strict type checking. Use when adding type annotations, implementing generic classes, defining structural interfaces, or configuring mypy/pyright.

    Also in 5 projects that use this

  • justanestaSkill

    python-error-handling

    Python error handling and debugging. Use this skill when implementing error handling, designing exception hierarchies, debugging issues, or adding logging. Covers exception patterns, custom exceptions, logging with structlog, debugging tools, and error recovery.

    Also in 4 projects that use this

  • CodeWithBehnamSkill

    python-anti-patterns

    Common Python anti-patterns to avoid. Use as a checklist when reviewing code, before finalizing implementations, or when debugging issues that might stem from known bad practices.

    Also in 4 projects that use this

  • xfstudioSkill

    python-testing-patterns

    Implement comprehensive testing strategies with pytest, fixtures, mocking, and test-driven development. Use when writing Python tests, setting up test suites, or implementing testing best practices.

    Also in 6 projects that use this

  • nilecuiSkill

    async-python-patterns

    Master Python asyncio, concurrent programming, and async/await patterns for high-performance applications. Use when building async APIs, concurrent systems, or I/O-bound applications requiring non-blocking operations.

    Also in 4 projects that use this

  • xfstudioSkill

    javascript-testing-patterns

    Implement comprehensive testing strategies using Jest, Vitest, and Testing Library for unit tests, integration tests, and end-to-end testing with mocking, fixtures, and test-driven development. Use when writing JavaScript/TypeScript tests, setting up test infrastructure, or implementing TDD/BDD workflows.

    Also in 3 projects that use this

  • +1 more from CodeWithBehnam
  • +2 more from xfstudio

The file

Show its rules and text

We split this skill into the rules it gives an agent

17 rules. 0 are common, 17 are distinctive, and 0 are very specific to this project.

  • 0

    Common

    Most of the 14 edited versions we read keep these.

  • 17

    Distinctive

    This author's own choices. Worth a look.

  • 0

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Use this skill to choose Python patterns that fit the codebase, runtime, and domain.

    line 8
  2. Distinctive:

    Prefer plain functions and cohesive modules until identity, lifecycle, invariants, substitution, or state justify classes or protocols.

    line 35
  3. Distinctive:

    Use structural typing and dependency injection at real boundaries, not for every collaborator.

    line 37
  4. Distinctive:

    Keep framework, ORM, request, response, serializer, and SDK types at adapters unless the project intentionally centers that framework.

    line 39
  5. Distinctive:

    Test domain rules with ordinary unit tests.

    line 41
  6. Distinctive:

    Dataclasses and frozen dataclasses: use for explicit data shapes, value objects, and immutable domain values.

    line 46
  7. Distinctive:

    Enums and literal types: use for small closed sets and states where string values would otherwise drift.

    line 49
  8. Distinctive:

    Protocols: use for outbound ports, plugin surfaces, or collaborators where structural substitution matters.

    line 51
  9. Distinctive:

    Context managers: use for resources that need reliable setup and teardown: files, locks, transactions, temporary directories, spans, and …

    line 53
  10. Distinctive:

    Factories and constructors: use factory functions or classmethods when creation has validation, defaults, environment lookup, or dependency …

    line 55
  11. Distinctive:

    Adapter modules: put HTTP clients, database access, queues, filesystems, CLIs, and framework handlers behind boundary modules that …

    line 57
  12. Distinctive:

    Repository pattern: use only when persistence shape should be hidden from domain/application policy.

    line 60
  13. Distinctive:

    Functional core, imperative shell: keep parsing, validation, decisions, and transformations pure where practical.

    line 62
  14. Distinctive:

    Pytest fixtures/builders: use fixtures for resource setup and builders for readable test data, with explicit cleanup and deterministic …

    line 64
  15. Distinctive:

    Prefer a supervised task group when sibling tasks share a lifetime: the parent creates them, observes failures, propagates shutdown, and …

    line 69
  16. Distinctive:

    DDD: use value objects, entities with identity, domain services, policies, and explicit state transitions where they protect real rules.

    line 82
  17. Distinctive:

    TDD: write focused tests for parsers, validators, policies, state changes, and error mapping before wiring broad integrations.

    line 89

“Common” is measured: kept by at least 6 in 10 of the 14 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Python Design Patterns

Use this skill to choose Python patterns that fit the codebase, runtime, and domain. Prefer simple modules, functions, data types, protocols, and explicit boundaries over pattern catalogs copied from class-heavy languages.

Use When

  • Python code needs a domain model, adapter boundary, test seam, construction pattern, resource-lifetime pattern, or API structure.
  • A refactor should make behavior easier to test without real frameworks, databases, filesystems, or services.
  • Clean, Hexagonal, Onion, DDD, BDD, or TDD guidance needs Python-specific expression.
  • Review feedback asks for a more idiomatic Python design.

For smell-focused review, use python-antipatterns. For package, uv, typing, test, and command workflow, use python-engineering.

Advice-only pattern choice may use this skill independently. When applying or implementing a pattern, load python-engineering.

Selection Rules

  1. Inspect local Python version, package layout, tests, framework conventions, typing policy, and existing data-model choices.
  2. Prefer plain functions and cohesive modules until identity, lifecycle, invariants, substitution, or state justify classes or protocols.
  3. Use structural typing and dependency injection at real boundaries, not for every collaborator.
  4. Keep framework, ORM, request, response, serializer, and SDK types at adapters unless the project intentionally centers that framework.
  5. Test domain rules with ordinary unit tests; test adapters with real framework, database, filesystem, or service contracts.

Patterns To Prefer

  • Dataclasses and frozen dataclasses: use for explicit data shapes, value objects, and immutable domain values. Add validation constructors when invalid states matter.
  • Enums and literal types: use for small closed sets and states where string values would otherwise drift.
  • Protocols: use for outbound ports, plugin surfaces, or collaborators where structural substitution matters. Keep protocols small and behavior-oriented.
  • Context managers: use for resources that need reliable setup and teardown: files, locks, transactions, temporary directories, spans, and test fixtures.
  • Factories and constructors: use factory functions or classmethods when creation has validation, defaults, environment lookup, or dependency wiring.
  • Adapter modules: put HTTP clients, database access, queues, filesystems, CLIs, and framework handlers behind boundary modules that translate to domain concepts.
  • Repository pattern: use only when persistence shape should be hidden from domain/application policy. Do not create generic CRUD repositories by default.
  • Functional core, imperative shell: keep parsing, validation, decisions, and transformations pure where practical; keep I/O at the edges.
  • Pytest fixtures/builders: use fixtures for resource setup and builders for readable test data, with explicit cleanup and deterministic values.

Async Ownership Patterns

  • Prefer a supervised task group when sibling tasks share a lifetime: the parent creates them, observes failures, propagates shutdown, and awaits every child before leaving its resource scope.
  • Give each queue, finite worker set, and the resources those workers use one explicit lifecycle owner. A task that must outlive a caller needs a named, longer-lived supervisor rather than becoming detached work.

Route cancellation, backpressure, overload, retry, ordering, recovery, and test workflow to python-engineering and its asyncio concurrency reference.

Architecture And Methods

  • DDD: use value objects, entities with identity, domain services, policies, and explicit state transitions where they protect real rules.
  • Clean / Hexagonal / Onion: represent use cases as functions or application service classes; use protocols for outbound ports when tests or adapters need substitution; keep framework handlers thin.
  • BDD: express externally observable workflows with domain language before choosing pytest, behave, pytest-bdd, or plain test names.
  • TDD: write focused tests for parsers, validators, policies, state changes, and error mapping before wiring broad integrations.

Review Checklist

  • Does the pattern solve a current behavior, boundary, or testing problem?
  • Are type hints meaningful without replacing runtime validation at trust boundaries?
  • Are exceptions specific and preserved with raise ... from exc where useful?
  • Are resources closed deterministically through context managers, fixtures, or framework lifecycle hooks?
  • Are framework and persistence models kept out of core domain APIs where that boundary matters?
  • Are mocks limited to external, slow, nondeterministic, or infrastructure boundaries?

Common Mistakes

  • Adding abstract base classes or protocols for one concrete implementation.
  • Turning simple functions into manager/service classes with no state or boundary.
  • Treating Pydantic, ORM, or serializer models as domain objects by default.
  • Hiding environment, filesystem, network, or database access in import-time side effects.
  • Using fixtures or monkeypatching to replace the behavior being specified.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.