|
3 | 3 | # pyright: reportMissingParameterType=false |
4 | 4 | # pyright: reportUnknownArgumentType=false |
5 | 5 | # pyright: reportUnknownLambdaType=false |
| 6 | +import typing |
6 | 7 | from collections.abc import Callable |
7 | 8 | from dataclasses import dataclass |
8 | 9 | from typing import TYPE_CHECKING, Annotated, Any, Final, NamedTuple, NotRequired, Required, TypedDict |
@@ -293,6 +294,18 @@ def func_dict_list() -> dict[str, list[int]]: # pragma: no cover |
293 | 294 | "additionalProperties": {"type": "array", "items": {"type": "integer"}}, |
294 | 295 | } |
295 | 296 |
|
| 297 | + def func_typing_dict() -> typing.Dict[str, str]: # noqa: UP006 |
| 298 | + return {"name": "John"} |
| 299 | + |
| 300 | + meta = func_metadata(func_typing_dict) |
| 301 | + assert meta.output_schema == { |
| 302 | + "type": "object", |
| 303 | + "additionalProperties": {"type": "string"}, |
| 304 | + } |
| 305 | + result = meta.convert_result(func_typing_dict()) |
| 306 | + assert isinstance(result, CallToolResult) |
| 307 | + assert result.structured_content == {"name": "John"} |
| 308 | + |
296 | 309 | # Test dict[int, str] - should be wrapped since key is not str |
297 | 310 | def func_dict_int_key() -> dict[int, str]: # pragma: no cover |
298 | 311 | return {1: "a", 2: "b"} |
@@ -762,6 +775,21 @@ def func_optional() -> str | None: # pragma: no cover |
762 | 775 | "required": ["result"], |
763 | 776 | } |
764 | 777 |
|
| 778 | + UserId = typing.NewType("UserId", int) |
| 779 | + |
| 780 | + def func_new_type() -> UserId: |
| 781 | + return UserId(1) |
| 782 | + |
| 783 | + meta = func_metadata(func_new_type) |
| 784 | + assert meta.output_schema == { |
| 785 | + "type": "object", |
| 786 | + "properties": {"result": {"type": "integer"}}, |
| 787 | + "required": ["result"], |
| 788 | + } |
| 789 | + result = meta.convert_result(func_new_type()) |
| 790 | + assert isinstance(result, CallToolResult) |
| 791 | + assert result.structured_content == {"result": 1} |
| 792 | + |
765 | 793 |
|
766 | 794 | def test_structured_output_dataclass(): |
767 | 795 | """Test structured output with dataclass return types""" |
|
0 commit comments