chore(typing): replace deprecated and legacy types#1378
Open
qwertycxz wants to merge 2 commits intoagentscope-ai:mainfrom
Open
chore(typing): replace deprecated and legacy types#1378qwertycxz wants to merge 2 commits intoagentscope-ai:mainfrom
qwertycxz wants to merge 2 commits intoagentscope-ai:mainfrom
Conversation
AgentScope is now compatible with Python 3.10 and above, and it is good time to replace deprecated and legacy typings in the codebase. The main changes include: 1. import ABC collections from `collections.abc` (e.g., `collections.abc.Iterable` instead of `typing.Iterable`) 2. use standard collections instead of those from `typing` (e.g., `list` instead of `typing.List`) 3. replace `typing.Union` and `typing.Optional` with `|` See: * https://peps.python.org/pep-0585 * https://peps.python.org/pep-0589 * https://peps.python.org/pep-0604
Contributor
There was a problem hiding this comment.
Pull request overview
This PR modernizes AgentScope’s type annotations for its Python 3.10+ baseline by replacing legacy/deprecated typing constructs with built-in generics, collections.abc ABCs, and PEP 604 unions—without intending runtime behavior changes.
Changes:
- Replaced
typing.List/Dict/Tuple/...with built-inlist/dict/tuple/...and moved ABC imports tocollections.abc. - Replaced
typing.Optional/UnionwithT | None/A | Bacross code, tests, docs, and examples. - Updated related docstrings/type aliases to match the new annotation style.
Reviewed changes
Copilot reviewed 120 out of 120 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/tuner_test.py | Update legacy typing annotations in tuner tests |
| tests/tts_openai_test.py | Use collections.abc.AsyncGenerator in tests |
| tests/tts_gemini_test.py | Use collections.abc.AsyncGenerator in tests |
| tests/tts_dashscope_test.py | Use collections.abc.AsyncGenerator in tests |
| tests/tts_dashscope_cosyvoice_test.py | Use collections.abc.AsyncGenerator in tests |
| tests/tracing_test.py | Move generator ABCs to collections.abc |
| tests/tracing_converter_test.py | Switch Dict to dict[...] in test types |
| tests/toolkit_middleware_test.py | Move ABC imports to collections.abc |
| tests/toolkit_basic_test.py | Replace Optional/Union/Tuple with ` |
| tests/session_test.py | Replace Union[...] with ` |
| tests/rag_store_test.py | Use collections.abc.AsyncGenerator in tests |
| tests/model_openai_test.py | Use collections.abc.AsyncGenerator in tests |
| tests/model_ollama_test.py | Use collections.abc.AsyncGenerator in tests |
| tests/model_gemini_test.py | Use collections.abc.AsyncGenerator in tests |
| tests/model_dashscope_test.py | Use collections.abc.AsyncGenerator in tests |
| tests/model_anthropic_test.py | Use collections.abc.AsyncGenerator in tests |
| tests/evaluation_test.py | Move Callable/Generator to collections.abc |
| tests/a2a_agent_test.py | Move async iterator ABC to collections.abc |
| src/agentscope/types/_tool.py | Modernize ToolFunction alias typing |
| src/agentscope/types/_object.py | Replace List[float] with list[float] |
| src/agentscope/types/_json.py | Replace Union types with PEP 604 unions |
| src/agentscope/tuner/prompt_tune/_tune_prompt.py | Replace Optional with ` |
| src/agentscope/tuner/prompt_tune/_config.py | Replace Optional[Literal...] with `Literal[...] |
| src/agentscope/tuner/model_selection/_model_selection.py | Replace legacy typing with built-ins/PEP 604 unions |
| src/agentscope/tuner/model_selection/_built_in_judges.py | Replace Dict[...] with dict[...] |
| src/agentscope/tuner/_workflow.py | Replace Dict with dict and move ABC imports |
| src/agentscope/tuner/_model.py | Replace Dict[...] return types with dict[...] |
| src/agentscope/tuner/_judge.py | Replace Dict with dict and move ABC imports |
| src/agentscope/tuner/_dataset.py | Replace List return type with list |
| src/agentscope/tuner/_config.py | Replace Tuple/List with tuple/list, move Callable |
| src/agentscope/tts/_utils.py | Move AsyncGenerator to collections.abc |
| src/agentscope/tts/_tts_base.py | Move AsyncGenerator to collections.abc |
| src/agentscope/tts/_openai_tts_model.py | Move AsyncGenerator to collections.abc |
| src/agentscope/tts/_gemini_tts_model.py | Move generator ABCs to collections.abc |
| src/agentscope/tts/_dashscope_tts_model.py | Move generator ABCs to collections.abc |
| src/agentscope/tts/_dashscope_realtime_tts_model.py | Move AsyncGenerator to collections.abc |
| src/agentscope/tts/_dashscope_cosyvoice_tts_model.py | Move AsyncGenerator to collections.abc |
| src/agentscope/tts/_dashscope_cosyvoice_realtime_tts_model.py | Move AsyncGenerator to collections.abc |
| src/agentscope/tracing/_trace.py | Move ABCs to collections.abc |
| src/agentscope/tracing/_extractor.py | Replace Dict/Tuple with dict/tuple and ` |
| src/agentscope/tracing/_converter.py | Replace Dict with dict in types/logic |
| src/agentscope/tool/_types.py | Replace Type[...] with type[...], move ABC imports |
| src/agentscope/tool/_toolkit.py | Move ABCs to collections.abc, replace Type with type |
| src/agentscope/tool/_response.py | Replace List/Optional with list/` |
| src/agentscope/tool/_multi_modality/_dashscope_tools.py | Move Sequence to collections.abc |
| src/agentscope/tool/_async_wrapper.py | Move ABCs to collections.abc |
| src/agentscope/realtime/_events/_client_event.py | Replace List[...] with list[...] |
| src/agentscope/rag/_store/_oceanbase_store.py | Move Callable to collections.abc |
| src/agentscope/plan/_plan_notebook.py | Move ABCs to collections.abc |
| src/agentscope/pipeline/_functional.py | Move ABCs to collections.abc, replace tuples with tuple[...] |
| src/agentscope/module/_state_module.py | Move Callable to collections.abc, replace Optional |
| src/agentscope/model/_trinity_model.py | Replace Optional[bool] with `bool |
| src/agentscope/model/_openai_model.py | Move AsyncGenerator to collections.abc, replace Type with type |
| src/agentscope/model/_ollama_model.py | Move async ABCs to collections.abc, replace Type with type |
| src/agentscope/model/_model_response.py | Move Sequence to collections.abc |
| src/agentscope/model/_model_base.py | Move AsyncGenerator to collections.abc |
| src/agentscope/model/_gemini_model.py | Move async ABCs to collections.abc, replace Type with type |
| src/agentscope/model/_dashscope_model.py | Move generator ABCs to collections.abc, replace unions with ` |
| src/agentscope/model/_anthropic_model.py | Move AsyncGenerator to collections.abc, replace Type with type |
| src/agentscope/message/_message_block.py | Replace List[...] with list[...] in message block types |
| src/agentscope/message/_message_base.py | Move Sequence to collections.abc, replace List with list |
| src/agentscope/memory/_long_term_memory/_mem0/_mem0_utils.py | Replace List/Dict with built-ins, move Coroutine |
| src/agentscope/mcp/_stateful_client_base.py | Replace List[...] with list[...] |
| src/agentscope/mcp/_mcp_function.py | Move Callable to collections.abc |
| src/agentscope/mcp/_http_stateless_client.py | Move ABCs to collections.abc, replace List with list |
| src/agentscope/mcp/_client_base.py | Move Callable to collections.abc, replace List with list |
| src/agentscope/formatter/_truncated_formatter_base.py | Move AsyncGenerator to collections.abc, replace Tuple with tuple |
| src/agentscope/formatter/_formatter_base.py | Move Sequence to collections.abc, replace List/Tuple with built-ins |
| src/agentscope/evaluate/_evaluator_storage/_file_evaluator_storage.py | Move Callable to collections.abc |
| src/agentscope/evaluate/_evaluator_storage/_evaluator_storage_base.py | Move Callable to collections.abc |
| src/agentscope/evaluate/_evaluator/_ray_evaluator.py | Move ABCs to collections.abc |
| src/agentscope/evaluate/_evaluator/_in_memory_exporter.py | Move Sequence to collections.abc |
| src/agentscope/evaluate/_evaluator/_general_evaluator.py | Move ABCs to collections.abc |
| src/agentscope/evaluate/_evaluator/_evaluator_base.py | Move ABCs to collections.abc |
| src/agentscope/evaluate/_benchmark_base.py | Move Generator to collections.abc |
| src/agentscope/evaluate/_ace_benchmark/_ace_tools_zh.py | Move Callable to collections.abc |
| src/agentscope/evaluate/_ace_benchmark/_ace_benchmark.py | Move Generator to collections.abc |
| src/agentscope/embedding/_openai_embedding.py | Replace List[...] with list[...] |
| src/agentscope/embedding/_ollama_embedding.py | Replace List[...] with list[...] |
| src/agentscope/embedding/_gemini_embedding.py | Replace List[...] with list[...] |
| src/agentscope/embedding/_file_cache.py | Replace List[...] with list[...] |
| src/agentscope/embedding/_embedding_response.py | Replace List[...] with list[...] |
| src/agentscope/embedding/_dashscope_embedding.py | Replace List[...] with list[...] |
| src/agentscope/embedding/_cache_base.py | Replace List[...] with list[...] |
| src/agentscope/agent/_user_input.py | Replace Type[...] with type[...] and modernize annotations |
| src/agentscope/agent/_user_agent.py | Replace Type[...] with type[...] |
| src/agentscope/agent/_react_agent_base.py | Move Callable to collections.abc |
| src/agentscope/agent/_react_agent.py | Replace Type[...] with type[...], move async ABC import |
| src/agentscope/agent/_agent_meta.py | Move Callable to collections.abc, replace Dict with dict |
| src/agentscope/agent/_agent_base.py | Move Callable to collections.abc |
| src/agentscope/agent/_a2a_agent.py | Replace Type[...] with type[...] |
| src/agentscope/_utils/_common.py | Replace Type/Dict with type/dict, move Callable |
| examples/tuner/model_tuning/main.py | Update example typing annotations |
| examples/tuner/model_selection/example_token_usage.py | Update example typing annotations |
| examples/tuner/model_selection/example_bleu.py | Update example typing annotations |
| examples/integration/qwen_deep_research_model/qwen_deep_research_agent.py | Replace Optional/Union with ` |
| examples/functionality/short_term_memory/reme/reme_short_term_memory.py | Replace List[...] with list[...] |
| examples/functionality/agent_skill/skill/analyzing-agentscope-library/view_agentscope_module.py | Move Callable to collections.abc |
| examples/evaluation/ace_bench/main.py | Move Callable to collections.abc |
| examples/deployment/planning_agent/tool.py | Move AsyncGenerator to collections.abc |
| examples/deployment/planning_agent/main.py | Move AsyncGenerator to collections.abc |
| examples/agent/meta_planner_agent/tool.py | Move AsyncGenerator to collections.abc |
| examples/agent/deep_research_agent/utils.py | Replace Union/Type with ` |
| examples/agent/deep_research_agent/deep_research_agent.py | Replace Optional/Tuple/Type with ` |
| examples/agent/browser_agent/build_in_helper/_video_understanding.py | Replace Optional/List with ` |
| examples/agent/browser_agent/browser_agent.py | Replace Optional/Type with ` |
| examples/agent/a2ui_agent/samples/general_agent/setup_a2ui_server.py | Move AsyncGenerator to collections.abc |
| examples/agent/a2a_agent/setup_a2a_server.py | Move AsyncGenerator to collections.abc |
| docs/tutorial/zh_CN/src/task_tuner.py | Update tutorial code typing annotations |
| docs/tutorial/zh_CN/src/task_tool.py | Move AsyncGenerator to collections.abc |
| docs/tutorial/zh_CN/src/task_middleware.py | Move AsyncGenerator/Callable to collections.abc |
| docs/tutorial/zh_CN/src/task_hook.py | Update tutorial typing annotations |
| docs/tutorial/zh_CN/src/task_eval_openjudge.py | Move Generator/Callable to collections.abc |
| docs/tutorial/zh_CN/src/task_eval.py | Move Generator/Callable to collections.abc |
| docs/tutorial/en/src/task_tuner.py | Update tutorial code typing annotations |
| docs/tutorial/en/src/task_tool.py | Move AsyncGenerator to collections.abc |
| docs/tutorial/en/src/task_middleware.py | Move AsyncGenerator/Callable to collections.abc |
| docs/tutorial/en/src/task_hook.py | Update tutorial typing annotations |
| docs/tutorial/en/src/task_eval_openjudge.py | Move Generator/Callable to collections.abc |
| docs/tutorial/en/src/task_eval.py | Move Generator/Callable to collections.abc |
Author
|
Need I to adjust config of mypy to prevent committing legacy typing in the future? If so, mypy need to be updated. 是否需要我调整 mypy 等的配置,以阻止这些旧版类型未来被提交?得升级 mypy 版本。 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
AgentScope Version
v1.0.18
Description
AgentScope is now compatible with Python 3.10 and above, and it is good time to replace deprecated and legacy typings in the codebase. The main changes include:
collections.abc(e.g.,collections.abc.Iterableinstead oftyping.Iterable)typing(e.g.,listinstead oftyping.List)typing.Unionandtyping.Optionalwith|See:
Please excuse me for such a big commit. But there are no changes in runtime.
Chinese
既然 AgentScope 已经面向 Python 3.10 了,就没必要保留旧版或被废弃的类型标注了。本 PR 主要改动:
collections.abc引入容器抽象基类(例如,以collections.abc.Iterable代替typing.Iterable)list代替typing.List)|代替typing.Union和typing.Optional参见:
一个包含 120 个文件的 PR 的确有点大,对 Code Review 造成的压力表示歉意;但所有的更改都很简单,运行时代码没有任何变动。
Checklist
pre-commit run --all-filescommand