π The fast, Pythonic way to build MCP servers and clients.
Fix partial hint resolution on Python 3.14 (#4796)
nate nowack6be0ac8e15d3
diff --git a/fastmcp_slim/fastmcp/tools/function_tool.py b/fastmcp_slim/fastmcp/tools/function_tool.pyindex e5787f466..b166cead7 100644--- a/fastmcp_slim/fastmcp/tools/function_tool.py+++ b/fastmcp_slim/fastmcp/tools/function_tool.py@@ -173,26 +173,26 @@ class ToolMeta:def _resolve_param_hints(fn: Callable[..., Any]) -> dict[str, Any]:"""Resolve a callable's parameter type hints, tolerating partials.- ``get_type_hints`` rejects ``functools.partial`` objects (and other- non-function callables), which the synchronous TypeAdapter path handles- natively. For those, resolve hints against the underlying function and keep- only the parameters that remain in the partially-bound signature.+ Depending on the Python version, ``get_type_hints`` either rejects+ ``functools.partial`` objects or returns no hints for them. The synchronous+ TypeAdapter path handles partials natively. Resolve their hints against the+ underlying function and keep only parameters in the partially-bound+ signature."""+ target = fn+ while isinstance(target, functools.partial):+ target = target.functry:- return get_type_hints(fn, include_extras=True)+ resolved = get_type_hints(target, include_extras=True)except TypeError:- target = fn- while isinstance(target, functools.partial):- target = target.func- try:- resolved = get_type_hints(target, include_extras=True)- except TypeError:- return {}- return {- name: resolved[name]- for name in inspect.signature(fn).parameters- if name in resolved- }+ return {}+ if target is fn:+ return resolved+ return {+ name: resolved[name]+ for name in inspect.signature(fn).parameters+ if name in resolved+ }class FunctionTool(Tool):diff --git a/tests/tasks/server/test_task_tools.py b/tests/tasks/server/test_task_tools.pyindex 2ffd0f192..0f7ed3c09 100644--- a/tests/tasks/server/test_task_tools.py+++ b/tests/tasks/server/test_task_tools.py@@ -12,11 +12,13 @@ from __future__ import annotationsimport asyncioimport functools+from typing import Anyimport pytestfrom fastmcp_tasks.models import CreateTaskResultfrom pydantic import BaseModel+import fastmcp.tools.function_tool as function_toolfrom fastmcp import FastMCPfrom fastmcp.exceptions import ValidationErrorfrom fastmcp.tools.function_tool import _resolve_param_hints@@ -143,20 +145,32 @@ async def test_valid_argument_submits_under_strict_validation():assert final.result["structuredContent"] == {"result": 16}-def test_resolve_param_hints_handles_partials():- """Partials aren't introspectable by get_type_hints; resolve via the func.+def test_resolve_param_hints_handles_partials(monkeypatch: pytest.MonkeyPatch):+ """Resolve partials via their function when direct introspection is empty.Argument coercion must not raise for partial-wrapped callables β it should- resolve hints for the still-unbound parameters.+ resolve hints for the still-unbound parameters. Python 3.14 returns an empty+ mapping instead of raising ``TypeError`` for the partial itself."""async def base(prefix: str, items: list[_Item]) -> str:return prefixpartial_fn = functools.partial(base, "bound")+ get_type_hints = function_tool.get_type_hints++ def python_314_get_type_hints(+ obj: object, *, include_extras: bool = False+ ) -> dict[str, Any]:+ if isinstance(obj, functools.partial):+ return {}+ return get_type_hints(obj, include_extras=include_extras)++ monkeypatch.setattr(function_tool, "get_type_hints", python_314_get_type_hints)hints = _resolve_param_hints(partial_fn)assert hints["items"] == list[_Item]+ assert "prefix" not in hints# ---------------------------------------------------------------------------