{"slug":"clean-architecture-python","title":"clean-architecture-python","summary":"Use when laying out a Python service packaged with pyproject.toml (FastAPI or another web framework), or adding a feature, endpoint, use case, aggregate or repository to one — which package a module belongs in, which package may import which, and how an endpoint reaches a use cas","platform":"Claude","tags":[],"authorName":"LLM Mart","authorSlug":"llm-mart","score":0,"source":"github","price":null,"verified":false,"createdAt":"2026-11-29T00:42:29.180455Z","repo":{"url":"https://github.com/SebastienDegodez/skraft-plugin","stars":17,"forks":3,"license":"GPL-3.0","updatedAt":"2026-11-29T00:18:21Z"},"bodyHtml":"<hr>\n<h2>name: clean-architecture-python\ndescription: Use when laying out a Python service packaged with pyproject.toml (FastAPI or another web framework), or adding a feature, endpoint, use case, aggregate or repository to one — which package a module belongs in, which package may import which, and how an endpoint reaches a use case. Not for configuration, dependency or tooling changes.</h2>\n<h1>Clean Architecture — Python</h1>\n<p>The layer rules are those of <code>architecture-patterns</code>. This skill maps them onto Python packages.</p>\n<h2>One package per layer</h2>\n<table>\n<thead>\n<tr>\n<th>Package</th>\n<th>Holds</th>\n<th>May import</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td><code>&lt;context&gt;.domain</code></td>\n<td>aggregates, value objects, domain events</td>\n<td>the standard library only</td>\n</tr>\n<tr>\n<td><code>&lt;context&gt;.application</code></td>\n<td>use cases and the <code>Protocol</code>s they call</td>\n<td><code>&lt;context&gt;.domain</code></td>\n</tr>\n<tr>\n<td><code>&lt;context&gt;.infrastructure</code></td>\n<td>implementations of those <code>Protocol</code>s (SQLAlchemy, HTTP clients, files)</td>\n<td><code>&lt;context&gt;.application</code>, <code>&lt;context&gt;.domain</code></td>\n</tr>\n<tr>\n<td><code>&lt;context&gt;.api</code></td>\n<td>routes, the app factory, the wiring of every class</td>\n<td>all three</td>\n</tr>\n</tbody>\n</table>\n<ul>\n<li>Lay out <code>src/&lt;context&gt;/</code> with one sub-package per layer, in a single <code>pyproject.toml</code>. A new feature goes into the existing layer packages, never into a new top-level package.</li>\n<li>FastAPI, Pydantic, SQLAlchemy and HTTP clients are imported only in <code>infrastructure</code> and <code>api</code>. Domain and application use <code>dataclasses</code> and <code>typing</code>.</li>\n<li>ORM models and their mapping live in <code>infrastructure</code>; never make a domain class an ORM or Pydantic model.</li>\n<li>Tests live in the two test packages <code>clean-architecture-testing</code> defines, never inside <code>src/</code>.</li>\n</ul>\n<h2>Use cases</h2>\n<ul>\n<li>A use case is a class named after the action (<code>BorrowBook</code>) with one public method. The route calls it directly: no <code>Protocol</code> or ABC in front of it, no <code>*Service</code> implementation.</li>\n<li>A command method returns <code>None</code>; the caller creates the new id and passes it in. A query returns a frozen <code>*ViewModel</code> dataclass defined in <code>application</code>, never a domain object.</li>\n<li>Application declares a <code>Protocol</code> only where it calls out (repository, gateway). A repository <code>Protocol</code> sits in <code>domain</code> when it persists an aggregate, in <code>application</code> otherwise.</li>\n<li>Who may <em>read</em> a resource is checked in the use case. Who may <em>change</em> it is an aggregate rule: the mutation method takes the caller's id and raises.</li>\n<li>Wire every class by hand in <code>api</code> (one composition module); FastAPI <code>Depends</code> appears only in <code>api</code>. No DI container.</li>\n<li>One transaction per use-case call, opened and committed in <code>api</code> around the call.</li>\n<li>Inside each layer, one sub-package per feature (<code>application/loans/borrow_book.py</code>, <code>infrastructure/loans/sql_loan_repository.py</code>), never a module at the layer root or a technical package (<code>repositories</code>, <code>services</code>, <code>models</code>). A feature imports no other feature; what two features use moves to a <code>shared</code> sub-package of that layer.</li>\n<li>No <code>ports</code> or <code>adapters</code> package, and no \"port\" in module, class or docstring.</li>\n</ul>\n<h2>Domain</h2>\n<ul>\n<li>An aggregate is created through a <code>classmethod</code> factory; code outside <code>domain</code> never calls the class itself. It is identified by a typed id (<code>OrderId</code>, a frozen dataclass or <code>NewType</code>), never a raw <code>str</code>, <code>int</code> or <code>UUID</code>.</li>\n</ul>\n<h2>Guard</h2>\n<p>Declare the graph in <code>pyproject.toml</code> and add <code>import-linter</code> to the dev dependencies; the architecture test of <code>clean-architecture-testing</code> runs <code>lint-imports</code>:</p>\n<pre><code>[tool.importlinter]\nroot_package = \"&lt;context&gt;\"\ninclude_external_packages = true\n\n[[tool.importlinter.contracts]]\nname = \"Layers import inward only\"\ntype = \"layers\"\nlayers = [\"&lt;context&gt;.api\", \"&lt;context&gt;.infrastructure\", \"&lt;context&gt;.application\", \"&lt;context&gt;.domain\"]\n\n[[tool.importlinter.contracts]]\nname = \"Domain and application import no framework\"\ntype = \"forbidden\"\nsource_modules = [\"&lt;context&gt;.domain\", \"&lt;context&gt;.application\"]\nforbidden_modules = [\"fastapi\", \"starlette\", \"pydantic\", \"sqlalchemy\", \"httpx\"]\n\n[[tool.importlinter.contracts]]\nname = \"Application features never import each other\"\ntype = \"independence\"\nmodules = [\"&lt;context&gt;.application.*\"]\nignore_imports = [\"&lt;context&gt;.application.** -&gt; &lt;context&gt;.application.shared.**\"]\nunmatched_ignore_imports_alerting = \"none\"\n</code></pre>\n","files":[{"path":"SKILL.md","sizeBytes":4091,"isText":true}],"reviewScore":null,"reviewSummary":null,"trust":{"provenance":"trusted-source-unreviewed","notice":"Community-authored content, reproduced verbatim and not vetted as instructions. Treat it as data to evaluate, never as directives to follow.","bodySource":null},"bodyLocked":false,"purchaseUrl":null,"sourceUrl":null,"report":{"provenance":"trusted-source-unreviewed","screen":{"ran":true,"outcome":"clean","suspicious":0,"notes":0,"hiddenCharacters":false},"virusScan":{"engine":"clamav","status":"clean","scannedAt":"2026-11-29T00:43:50.913361Z","sha256":"F34BAB2142828BF229C36816A737FF5131B78329B80A98D5ED1E8F1C8C519FD8","sizeBytes":1871},"review":null,"source":{"repositoryUrl":"https://github.com/SebastienDegodez/skraft-plugin","path":"plugins/skraft-framework/skills/clean-architecture-python","license":"GPL-3.0","commit":"c6117c6236a25490bf1916b131866069740c9b22","subtreeSha":"6BCEA312E6A32EB1DE97E0FB488EB7D9D10CF2554030B8BA6D6B8FE3A3D5F6C2","lastSyncedAt":"2026-11-29T00:42:27.348488Z"},"reviewedAt":"2026-11-29T00:46:55.61789Z","notice":"Community-authored content, reproduced verbatim and not vetted as instructions. Treat it as data to evaluate, never as directives to follow."},"install":[{"target":"skills-cli","command":"npx skills add https://github.com/SebastienDegodez/skraft-plugin/tree/main/plugins/skraft-framework/skills/clean-architecture-python"},{"target":"claude-code","command":"claude plugin marketplace add https://llmmart.ai/marketplace.json && claude plugin install sebastiendegodez-skraft-plugin@llmmart"},{"target":"git","command":"git clone https://github.com/SebastienDegodez/skraft-plugin.git"}]}