Skip to content

Commit 3e95d91

Browse files
CopilotOhYee
andcommitted
refactor(core): group packages by install command in _OPTIONAL_PACKAGES
Refactor _OPTIONAL_PACKAGES from a list of tuples to a dictionary that groups packages by their installation command. This provides better organization when multiple packages share the same installation command. Changes: - Changed from list of (package, install_cmd) tuples to dict of {install_cmd: [packages]} - Removed _SERVER_INSTALL_CMD constant (now a dict key) - Updated iteration logic to handle nested structure - All three server packages (fastapi, uvicorn, ag_ui) grouped together Benefits: - Clearer relationship: install command -> packages it provides - More compact when packages share the same install command - Easier to see all packages for a given installation - Scalable for future optional dependency groups Addresses feedback from @OhYee about merging packages with the same command. Co-authored-by: OhYee <[email protected]>
1 parent 9bbe850 commit 3e95d91

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed

agentrun/__init__.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -289,15 +289,12 @@
289289
"MergeOptions",
290290
}
291291

292-
# 可选依赖包映射:导入错误的包名 -> 安装使用的包名
293-
# Optional dependency mapping: import error package name -> installation package name
294-
# 所有 server 相关的可选依赖 / All server-related optional dependencies
295-
_SERVER_INSTALL_CMD = "agentrun-sdk[server]"
296-
_OPTIONAL_PACKAGES = [
297-
("fastapi", _SERVER_INSTALL_CMD),
298-
("uvicorn", _SERVER_INSTALL_CMD),
299-
("ag_ui", _SERVER_INSTALL_CMD),
300-
]
292+
# 可选依赖包映射:安装命令 -> 导入错误的包名列表
293+
# Optional dependency mapping: installation command -> list of import error package names
294+
# 将使用相同安装命令的包合并到一起 / Group packages with the same installation command
295+
_OPTIONAL_PACKAGES = {
296+
"agentrun-sdk[server]": ["fastapi", "uvicorn", "ag_ui"],
297+
}
301298

302299

303300
def __getattr__(name: str):
@@ -314,13 +311,14 @@ def __getattr__(name: str):
314311
except ImportError as e:
315312
# 检查是否是缺少可选依赖导致的错误
316313
error_str = str(e)
317-
for package_name, install_name in _OPTIONAL_PACKAGES:
318-
if package_name in error_str:
319-
raise ImportError(
320-
f"'{name}' requires the 'server' optional dependencies. "
321-
f"Install with: pip install {install_name}\n"
322-
f"Original error: {e}"
323-
) from e
314+
for install_cmd, package_names in _OPTIONAL_PACKAGES.items():
315+
for package_name in package_names:
316+
if package_name in error_str:
317+
raise ImportError(
318+
f"'{name}' requires the 'server' optional dependencies. "
319+
f"Install with: pip install {install_cmd}\n"
320+
f"Original error: {e}"
321+
) from e
324322
# 其他导入错误继续抛出
325323
raise
326324

0 commit comments

Comments
 (0)