Lumary (企业级 FastAPI 基础框架)
Lumary 是一个基于 FastAPI 和 Pydantic V2 构建的高标准、生产级 Python Web 基础框架。它不仅提供了一套极其严苛的代码规范,还为开发者屏蔽了大量底层复杂的封装细节,实现了真正的“开箱即用”与“极致开发者体验(DX)”。
✨ 核心特性 (Key Features)
📦 1. 极致无侵入的自动响应包装 (Auto Response Wrapper)
彻底告别繁琐的 return {"code": 0, "data": ...}。
通过底层的 LumaryRoute 拦截器,开发者只需在路由函数中直接返回业务模型,框架会在序列化前自动将其包装为企业级的 APIResponse,并且 完美支持 Swagger OpenAPI Schema 推导!
- 支持普通数据直接返回。
- 支持
return data, extra元组自动解包并生成带有扩展字段的结构。 - 智能防重复包装,支持字典与
Response对象的透明放行。
🔗 2. 全链路 Request ID 追踪 (Traceability)
基于 Python contextvars 实现协程安全的请求上下文管理。
框架在入口中间件自动生成(或提取)X-Request-ID,并将其无感注入到:
- 每一条业务日志中(结合
logging.Filter)。 - 每一个 API 接口的统一响应体中 (
APIResponse.request_id)。
🛡️ 3. Pydantic V2 深度适配与全局序列化
完美迁移至 Pydantic V2 标准。
- 摒弃了被弃用的
json_encoders,采用@model_serializer(mode='wrap')实现了全局时间字段(datetime)的递归格式化(YYYY-MM-DD HH:MM:SS)。 - 严格遵循
Field定义与默认值约束。
🗄️ 4. 生产级异步 ORM 与 CRUD 泛型
基于 SQLAlchemy 2.0 的纯异步架构:
- 安全第一:修复了连接池与 Session 泄漏隐患(采用 AsyncGenerator 生命周期)。
- 泛型 CRUD:内置高度封装的
CRUDBase,支持自动分页 (PageData)、多条件查询、动态排序。 - 高级 Mixin:内置
SoftDeleteMixin(软删除)与AuditMixin(审计字段),自动在查询时过滤已软删除数据。
🚨 5. 异常收敛与统一错误链路
重构全局异常处理器(Exception Handlers),将 Pydantic 校验错误(RequestValidationError)和系统底层异常转化为标准的 HTTPException,再交由底层统一格式化,确保所有报错格式 100% 统一。
📏 6. 强迫症级的代码规范
全面遵循极其严苛的 CODING_STANDARDS.md:
import语句严格按照代码中被首次调用的顺序排列。- 极简的 Google 风格 Docstring(仅包含纯文本描述,剥离冗余的类型标注)。
🚀 快速开始 (Quick Start)
1. 安装 (Installation)
确保你的环境是 Python 3.10+,根据你的业务需求选择合适的安装方式:
# 最小安装(仅核心功能,无数据库依赖)
pip install lumary
# 仅 SQLAlchemy[asyncio] 支持(包含 python-ulid)
pip install lumary[sqlalchemy]
# 标准安装(包含核心功能 + SQLAlchemy支持 + 配置管理 pydantic-settings + 多文件上传 python-multipart)
pip install lumary[standard]
如果你是下载源码进行本地开发,可以使用 pip install -e .[standard]。
2. 编写业务路由
使用原生的 APIRouter,并注入 LumaryRoute 即可专注于业务逻辑:
from fastapi import FastAPI, APIRouter
from lumary import LumaryRoute
from pydantic import BaseModel
class UserOut(BaseModel):
name: str
age: int
# 实例化 APIRouter 并绑定 LumaryRoute 拦截器,它会自动包装响应
router = APIRouter(prefix="/users", tags=["用户模块"], route_class=LumaryRoute)
@router.get("/{user_id}", response_model=UserOut)
async def get_user(user_id: int):
# 直接返回数据模型,底层会自动包装!
return UserOut(name="张三", age=25)
3. 挂载并启动应用
from lumary import Lumary
# 使用封装好的 Lumary App
app = Lumary(
title="我的企业级应用",
version="1.0.0",
debug=True
)
app.include_router(router)
# 实际的 API 响应结果:
# {
# "request_id": "01J2X...",
# "code": 0,
# "message": "成功",
# "data": {
# "name": "张三",
# "age": 25
# }
# }
📂 核心目录结构
lumary/
├── common/ # 公共组件 (日志、工具、上下文)
├── db/ # 数据库封装 (SQLAlchemy, Mixins, CRUD基类)
├── ws/ # WebSocket 管理器
├── application.py # Lumary 核心 App 封装
├── exceptions.py # 统一业务与 HTTP 异常基类
├── handlers.py # 全局异常拦截与格式化转换
├── lifespan.py # 异步生命周期管理与事件钩子
├── middleware.py # 核心中间件 (Request ID 注入等)
├── router.py # 自动响应包装器 (LumaryRoute)
└── schemas.py # 全局通用数据模型与响应体配置
Metadata
Release files for lumary 0.2.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| lumary-0.2.3.tar.gz | 78.1 kB | With provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| lumary-0.2.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 141.9 kB
Release files / lumary-0.2.3.tar.gz
| Download URL | lumary-0.2.3.tar.gz |
|---|---|
| Size | 78.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8e888304095a8bc85ca484b809479fd8e922129ee94e887023da4eb18c6f5a59
|
|
BLAKE2b-256 checksum How to use checksums |
cb3a0de4785a9c3b3b77b91556a04fb6485db6fafbf03372fd03b15f67cd3a56
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jun 23, 2026.
Transparency logRelease files / lumary-0.2.3-py3-none-any.whl
| Download URL | lumary-0.2.3-py3-none-any.whl |
|---|---|
| Size | 63.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
4b903fd9c662e4ed7946a4ecb9dbff4bb612ec65bcc7afa6b1ae2dee292ce7ea
|
|
BLAKE2b-256 checksum How to use checksums |
d9c9f5d21a178e1a8e4910cd94acc9e4b35155ce97fd180b56d4ce24ae1da695
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jun 23, 2026.
Transparency logSecurity for lumary 0.2.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
All 2 files in this release have been uploaded with provenance metadata.
Attestation for lumary-0.2.3.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jun 23, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.