Skip to content

Python: Changes tools parameter type hints from MutableMapping to Mapping for broader type compatibility.#3867

Open
giles17 wants to merge 1 commit intomicrosoft:mainfrom
giles17:mutablemapping_fix
Open

Python: Changes tools parameter type hints from MutableMapping to Mapping for broader type compatibility.#3867
giles17 wants to merge 1 commit intomicrosoft:mainfrom
giles17:mutablemapping_fix

Conversation

@giles17
Copy link
Contributor

@giles17 giles17 commented Feb 11, 2026

Motivation and Context

The tools parameter accepts dict-based tool definitions that are only read, never mutated.
Resolves #3577.

Description

Contribution Checklist

  • The code builds clean without any errors or warnings
  • The PR follows the Contribution Guidelines
  • All unit tests pass, and I have added new tests where possible
  • Is this a breaking change? If yes, add "[BREAKING]" prefix to the title of the PR.

Copilot AI review requested due to automatic review settings February 11, 2026 21:48
@github-actions github-actions bot changed the title Changes tools parameter type hints from MutableMapping to Mapping for broader type compatibility. Python: Changes tools parameter type hints from MutableMapping to Mapping for broader type compatibility. Feb 11, 2026
@markwallace-microsoft
Copy link
Member

Python Test Coverage

Python Test Coverage Report •
FileStmtsMissCoverMissing
packages/core/agent_framework
   _agents.py3203589%474, 887, 924, 1024–1026, 1139, 1180, 1182, 1191–1196, 1202, 1204, 1214–1215, 1222, 1224–1225, 1233–1237, 1245–1246, 1248, 1253, 1255, 1289, 1329, 1349
TOTAL21097334484% 

Python Unit Test Overview

Tests Skipped Failures Errors Time
4061 225 💤 0 ❌ 0 🔥 1m 12s ⏱️

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 tools parameter annotations from MutableMapping[str, Any] to Mapping[str, Any] in RawAgent/Agent constructors and run() 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_context still types final_tools as ... | dict[str, Any] .... If callers pass a non-dict mapping (e.g., ChainMap/MappingProxyType), this becomes a type mismatch even though runtime behavior is fine. Consider widening final_tools (and any downstream tool list types) to Mapping[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]
Copy link

Copilot AI Feb 11, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
| Sequence[FunctionTool | Callable[..., Any] | Mapping[str, Any] | Any]
| list[FunctionTool | Callable[..., Any] | Mapping[str, Any] | Any]

Copilot uses AI. Check for mistakes.
@giles17 giles17 enabled auto-merge February 11, 2026 22:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: tools: Consider using Mapping instead of MutableMapping in tools parameter

2 participants