clean-architecture-python

Structures Python services into hexagonal architecture with separated layers and dependency injection.

6|1|Updated Apr 7, 2026
One-click install
npx skills add https://github.com/kmshihab7878/claude-code-setup --skill clean-architecture-python-kmshihab7878
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: clean-architecture-python
Source: https://github.com/kmshihab7878/claude-code-setup/tree/main/skills/clean-architecture-python
Command: npx skills add https://github.com/kmshihab7878/claude-code-setup --skill clean-architecture-python-kmshihab7878

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Many Python services become fragile when business logic, infrastructure, and transport concerns are mixed together, making code hard to test, change, and reason about. This Skill prescribes hexagonal/clean architecture and DDD patterns to keep the domain pure and dependencies inverted.

Core Features & Use Cases

  • Layered Structure: Defines domain, application, infrastructure, and API layers with inward-facing dependency rules.
  • Ports & Adapters: Encourages abstract ports (interfaces) and concrete adapters for persistence and external APIs.
  • Dependency Injection & Testing: Shows container wiring and testing strategies to enable unit tests for domain and use-case logic and integration tests for adapters.
  • Use Case Example: Implement an agent management service with an Agent entity, a repository port, a Postgres adapter, and FastAPI routes.

Quick Start

Generate a Python project scaffold that implements hexagonal clean architecture with domain, application, infrastructure, and FastAPI adapters wired via dependency injection.

Frequently Asked Questions about clean-architecture-python

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I structure a FastAPI service with hexagonal architecture in Python?

Structure Python services into hexagonal architecture by separating domain, application, infrastructure, and API layers. This enforces strict inward-facing dependency rules so FastAPI routes act as adapters without leaking infrastructure concerns into the business domain.

How do I keep the domain layer pure and free of external imports in Python?

Keep the domain layer pure by defining abstract ports and using concrete adapters for persistence and external APIs. Dependency inversion ensures your domain entities and use cases remain free of external imports like ORMs or framework specific dependencies.

How does dependency injection work in a clean architecture Python project?

Dependency injection in clean architecture works through container wiring to provide use cases with repository implementations. This allows you to isolate domain and application logic for unit testing while enabling integration testing for infrastructure adapters like Postgres.

What is the best way to test business logic separated from infrastructure in Python?

The best way to test business logic separated from infrastructure is by applying the repository pattern with abstract ports. This enables rapid unit tests for domain rules and use cases while isolating slow integration tests for database and API adapters.

When do I need clean architecture for my Python backend service?

You need clean architecture when your Python backend mixes business logic with infrastructure and transport concerns. If your service uses FastAPI, ORMs, and asynchronous IO and suffers from poor testability and maintainability, hexagonal architecture enforces necessary layer boundaries.

Can I use the repository pattern with asynchronous IO and ORMs in Python?

Yes, you can use the repository pattern with asynchronous IO and ORMs by implementing abstract repository ports and concrete database adapters. This keeps your domain layer free of ORM imports while supporting asynchronous database operations in the infrastructure layer.