python项目中pyproject.toml是做什么用的
pyproject.toml,相当于现代 Python 包管理的“说明书”。它定义了项目的基本信息、依赖库、打包规则、测试配置等。可以把它理解成 这个 A2A SDK 项目的元数据 + 环境说明 + 开发配置。我帮你逐段拆解解读:
⸻
- [project] —— 项目信息
[project]
name = "a2a-sdk"
dynamic = ["version"]
description = "A2A Python SDK"
readme = "README.md"
license = "Apache-2.0"
authors = [{ name = "Google LLC", email = "googleapis-packages@google.com" }]
requires-python = ">=3.10"
keywords = ["A2A", "A2A SDK", "A2A Protocol", "Agent2Agent", "Agent 2 Agent"]
• name:项目名是 a2a-sdk,也就是一个“Agent-to-Agent”协议的 Python SDK。
• dynamic = [“version”]:版本号不是写死的,而是通过 git 标签等方式动态生成。
• description / readme / license:描述信息、说明文档、开源协议。
• authors:作者(这里是 Google LLC)。
• requires-python:最低 Python 版本要求 >=3.10。
• keywords:关键词,用于 PyPI 搜索时能更容易找到。
⸻
- dependencies —— 核心依赖
dependencies = ["httpx>=0.28.1","httpx-sse>=0.4.0","pydantic>=2.11.3","protobuf>=5.29.5","google-api-core>=1.26.0",
]
表示 SDK 依赖的库:
• httpx / httpx-sse:异步 HTTP & Server-Sent Events,说明 SDK 会涉及流式通讯。
• pydantic:数据模型与校验。
• protobuf:协议数据格式(Google 的序列化方案)。
• google-api-core:谷歌 API 通用组件。
说明:SDK 本身是基于 网络通信 + 协议数据建模 来实现的。
⸻
- classifiers —— 分类标签
classifiers = ["Programming Language :: Python :: 3.10","Programming Language :: Python :: 3.13","License :: OSI Approved :: Apache Software License",
]
这是 PyPI 用的标准分类,标注了适用 Python 版本、开源许可证等。
⸻
- [project.optional-dependencies] —— 可选依赖
[project.optional-dependencies]
http-server = ["fastapi>=0.115.2", "sse-starlette", "starlette"]
postgresql = ["sqlalchemy[asyncio,postgresql-asyncpg]>=2.0.0"]
...
这些是可选功能模块:
• http-server:如果要启动 HTTP 服务,可以额外安装 FastAPI + SSE 支持。
• postgresql/mysql/sqlite/sql:不同数据库的异步驱动支持。
• encryption:加密功能(cryptography)。
• grpc:支持 gRPC 协议。
• telemetry:接入 OpenTelemetry 做监控和日志。
👉 用法:pip install a2a-sdk[postgresql] 就会安装带 PostgreSQL 的扩展。
⸻
- [project.urls] —— 项目链接
[project.urls]
homepage = "https://a2a-protocol.org/"
repository = "https://github.com/a2aproject/a2a-python"
官方主页、仓库地址、文档链接。
⸻
- [tool.hatch.*] —— 构建配置
[tool.hatch.build.targets.wheel]
packages = ["src/a2a"]
• 使用 hatchling 作为构建工具,指定打包目录是 src/a2a。
• 版本管理用 uv-dynamic-versioning,根据 git 生成 PEP440 风格版本号。
• sdist 打包时排除 tests/。
⸻
- [tool.pytest.*] —— 测试配置
[tool.pytest.ini_options]
testpaths = ["tests"]
python_files = "test_*.py"
定义了单元测试规则:
• 测试文件在 tests/ 目录。
• 测试函数必须以 test_ 开头。
• 使用 pytest-asyncio 严格模式,保证异步测试不混乱。
⸻
- [dependency-groups] —— 开发依赖
[dependency-groups]
dev = ["mypy", "pytest", "pytest-asyncio", "ruff", "pre-commit", ...
]
这是开发环境需要的工具:
• mypy / pyright:类型检查。
• pytest / pytest-asyncio:测试框架。
• ruff / autoflake / pyupgrade:代码规范、自动格式化。
• datamodel-code-generator:根据 JSON Schema 生成 pydantic 模型。
说明:项目有比较严格的 类型安全 + 自动化测试 + 代码质量 要求。
⸻
- [tool.uv.index] —— PyPI 测试源
[[tool.uv.index]]
name = "testpypi"
url = "https://test.pypi.org/simple/"
这让开发者可以把包先发布到 test PyPI 做测试,再发到正式 PyPI。
⸻
- [tool.mypy] & [tool.pyright] —— 类型检查
• pydantic.mypy 插件:增强 Pydantic 的类型推导。
• pyright:指定哪些文件夹检查,哪些忽略。
⸻
总结:怎么解读?
1. 这是一个 pyproject.toml 文件,作用是定义 Python 项目的依赖、构建、测试、发布规则。
2. 说明这个项目是 A2A 协议的 Python SDK,用于 Agent-to-Agent 通信,Google 参与开发。
3. 核心依赖是 httpx、protobuf、pydantic,说明它基于网络通信和数据建模。
4. 可选功能模块支持数据库、加密、gRPC、监控,说明它能扩展为一个完整的分布式 Agent 平台组件。
5. 开发环境配置很完整(mypy/pyright/ruff/pytest),体现了工程化和可维护性。