Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
P

py-clean-arch

> 后端框架
Open source

A Python implementation of Clean Architecture, inspired by Uncle Bob's book

197 stars0 likes2 views
WebsiteGitHub

About

A Python implementation of Clean Architecture, inspired by Uncle Bob's book

py-clean-arch

A Python architecture sample that demonstrates Uncle Bob's Clean Architecture through a Pokémon and Trainer API built with FastAPI. The focus is on showing how layers are separated, how dependencies flow inward, and how infrastructure can be swapped at the structural level.

This is a learning-oriented project, not a production template. For background and common questions, see the FAQ.

Architecture

This project applies Clean Architecture with the following practices:

  1. Framework Independence: FastAPI is confined to the controllers layer — use cases and models have no framework dependency.
  2. Testability: Business rules in use cases can be tested without HTTP or database.
  3. UI Independence: REST and GraphQL are interchangeable outer-layer interfaces.
  4. Database Independence: Multiple databases implement the same repository contracts.
  5. External Independence: Domain logic does not depend on specific APIs, ORMs, or drivers.

*source: yoan-thirion.gitbook.io

Project Structure

…

What This Sample Demonstrates

REST and GraphQL share the same use cases layer — the API protocol is an outer-layer detail. 5 databases (SQLite, MySQL, PostgreSQL, MongoDB, Redis) sit behind the same repository contracts — storage is also an outer-layer detail.

Two entities illustrate different complexity levels:

  • Pokemon — basic CRUD
  • Trainer — business rules like team size limits, ownership checks, and atomic trades

Enabled by several key design decisions:

  • Repository Pattern ^1 — decouples the model layer from data storage
  • Unit of Work Pattern ^2 — ensures transactional consistency
  • Dependency Injection ^3 — reduces coupling between modules
  • Asynchronous SQLAlchemy 2.0 ^4 — async database operations

Flow Diagrams

The flow diagrams visualize the layers and how they interact:

For a detailed explanation of the ASCII flow, refer to ascii-flow.md.

*source: yoan-thirion.gitbook.io

*source: https://stackoverflow.com/a/73788685

Getting Started

⚡︎ Quick Start

Start the application with a single command — no local Python or dependency installation required:

bash
$ docker compose up app

Uses in-memory SQLite by default. Access the API documentation at http://localhost:8000/docs and try the interactive endpoints.

Database Options

Set DATABASE_URI to switch databases:

Database URI format
SQLite (file) sqlite+aiosqlite:///.db
SQLite (memory) sqlite+aiosqlite:///:memory: (default)
MySQL mysql+asyncmy://:@:/
PostgreSQL postgresql+asyncpg://:@:/
MongoDB mongodb://:@:/
Redis redis://:@:/

Note: If you encounter database initialization issues, append reinitialize=true to the DATABASE_URI, e.g., sqlite+aiosqlite:///sqlite.db?reinitialize=true.

bash
$ docker compose down --remove-orphans -v
$ docker compose up dockerize

Development Setup

For local development with hot-reload:

  1. Install prerequisites: Python 3.11+ and uv

  2. Install dependencies: [^5]

    bash
    $ uv sync
  3. Launch the application:

    bash
    # With default SQLite database
    $ make up
    
    # With specific database
    $ DATABASE_URI= make up
  4. Access the application: http://localhost:8000

Testing

bash
# Test against a specific database
$ DATABASE_URI= uv run pytest

# Test against all databases (requires bats)
$ make test

For supported database URIs, see Database Options

Note: Use a different dbname with "_test" suffix for testing (e.g., "mydatabase_test") to avoid interfering with your main application data.

Changelog

See the full version history in CHANGELOG.md.

Enjoying the Project .ᐣ

If this project helped you, a ⭐ would be greatly appreciated!

Have questions or ideas? Feel free to open an issue or submit a PR.

[^5]: The uv sync command installs all required packages for running and developing the application. However, it does not include cspell. If you need cspell for spell checking, please refer to the official installation guide at cspell installation guide

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Pythonasyncioclean-architecturedependency-injectionfastapi

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
Category后端框架
PricingOpen source

> Related tools

N
Node.js
基于 V8 的 JavaScript 运行时
D
Django
Python 高级 Web 框架
S
Spring Boot
Java 生态主流微服务框架