Python: Changes tools parameter type hints from MutableMapping to Mapping for broader type compatibility.#3867
Python: Changes tools parameter type hints from MutableMapping to Mapping for broader type compatibility.#3867giles17 wants to merge 1 commit intomicrosoft:mainfrom
MutableMapping to Mapping for broader type compatibility.#3867Conversation
MutableMapping to Mapping for broader type compatibility.MutableMapping to Mapping for broader type compatibility.
Python Test Coverage Report •
Python Unit Test Overview
|
||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Pull request overview
Updates the Python agent API’s tools type hints to use Mapping instead of MutableMapping, reflecting that tool definitions are read-only and improving compatibility with more mapping types.
Changes:
- Switched
toolsparameter annotations fromMutableMapping[str, Any]toMapping[str, Any]inRawAgent/Agentconstructors andrun()overloads. - Updated internal casts and normalized tool list annotations to match the new mapping type.
Comments suppressed due to low confidence (1)
python/packages/core/agent_framework/_agents.py:1016
- After widening the accepted tool-definition type to
Mapping[str, Any],_prepare_run_contextstill typesfinal_toolsas... | dict[str, Any] .... If callers pass a non-dictmapping (e.g.,ChainMap/MappingProxyType), this becomes a type mismatch even though runtime behavior is fine. Consider wideningfinal_tools(and any downstream tool list types) toMapping[str, Any]for consistency with the new contract.
normalized_tools: list[FunctionTool | Callable[..., Any] | Mapping[str, Any] | Any] = (
[] if tools_ is None else tools_ if isinstance(tools_, list) else [tools_]
)
agent_name = self._get_agent_name()
# Resolve final tool list (runtime provided tools + local MCP server tools)
final_tools: list[FunctionTool | Callable[..., Any] | dict[str, Any] | Any] = []
for tool in normalized_tools:
if isinstance(tool, MCPTool):
| | Mapping[str, Any] | ||
| | Any | ||
| | Sequence[FunctionTool | Callable[..., Any] | MutableMapping[str, Any] | Any] | ||
| | Sequence[FunctionTool | Callable[..., Any] | Mapping[str, Any] | Any] |
There was a problem hiding this comment.
The tools parameter is typed as a Sequence[...], but normalization only treats list as a collection (isinstance(tools_, list)); passing a tuple/other sequence will be wrapped as a single item and the contained tools will be ignored downstream. Either restrict the type hint to list[...] (to match behavior) or update normalization/casts to accept any non-string Sequence and iterate over it.
| | Sequence[FunctionTool | Callable[..., Any] | Mapping[str, Any] | Any] | |
| | list[FunctionTool | Callable[..., Any] | Mapping[str, Any] | Any] |
|
Found some additional work that needed to be done with this param, so fixing that in a new PR. CC @giles17 |
Pull request was closed
Motivation and Context
The tools parameter accepts dict-based tool definitions that are only read, never mutated.
Resolves #3577.
Description
Contribution Checklist