File: test_functions.py

package info (click to toggle)
anthropic-sdk-python 0.76.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,432 kB
  • sloc: python: 30,183; sh: 186; makefile: 5
file content (447 lines) | stat: -rw-r--r-- 16,953 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
from __future__ import annotations

from typing import Any, cast

import pytest
from pydantic import BaseModel

from anthropic import beta_tool
from anthropic._compat import PYDANTIC_V1
from anthropic.lib.tools._beta_functions import BaseFunctionTool
from anthropic.types.beta.beta_tool_param import InputSchema


@pytest.mark.skipif(PYDANTIC_V1, reason="only applicable in pydantic v2")
class TestFunctionTool:
    def test_basic_function_schema_conversion(self) -> None:
        """Test basic function schema conversion with simple types."""

        def get_weather(location: str, unit: str = "celsius") -> str:
            """Get the weather for a specific location."""
            return f"Weather in {location} is 20 degrees {unit}"

        function_tool = beta_tool(get_weather)

        assert function_tool.name == "get_weather"
        assert function_tool.description == "Get the weather for a specific location."
        assert function_tool.input_schema == {
            "additionalProperties": False,
            "type": "object",
            "properties": {
                "location": {"title": "Location", "type": "string"},
                "unit": {"title": "Unit", "type": "string", "default": "celsius"},
            },
            "required": ["location"],
        }
        assert function_tool(location="CA") == "Weather in CA is 20 degrees celsius"

        # invalid types should be allowed because __call__ should just be the original function
        assert function_tool(location=cast(Any, 1)) == "Weather in 1 is 20 degrees celsius"

    def test_function_with_multiple_types(self) -> None:
        """Test function schema conversion with various Python types."""

        def simple_function(
            name: str,
            age: int,
        ) -> str:
            """A simple function with basic parameter types."""
            return f"Person: {name}, {age} years old"

        function_tool = beta_tool(simple_function)

        # Test that we can create the tool and call it
        assert function_tool.name == "simple_function"
        assert function_tool.description == "A simple function with basic parameter types."

        # Test calling the function
        result = function_tool.call(
            {
                "name": "John",
                "age": 25,
            }
        )
        assert result == "Person: John, 25 years old"

        # Test schema structure
        expected_schema = {
            "additionalProperties": False,
            "type": "object",
            "properties": {
                "name": {"title": "Name", "type": "string"},
                "age": {"title": "Age", "type": "integer"},
            },
            "required": ["name", "age"],
        }

        assert function_tool.input_schema == expected_schema

    def test_function_call_with_valid_input(self) -> None:
        def add_numbers(a: int, b: int) -> str:
            """Add two numbers together."""
            return str(a + b)

        function_tool = beta_tool(add_numbers)
        result = function_tool.call({"a": 5, "b": 3})

        assert result == "8"

    @pytest.mark.parametrize(
        "input_data, expected_error_type, expected_error_msg",
        [
            pytest.param(
                {"a": "not a number", "b": 3},
                ValueError,
                "Invalid arguments for function add_numbers",
                id="invalid_argument_type",
            ),
            pytest.param(
                {"b": 3},
                ValueError,
                "Invalid arguments for function add_numbers",
                id="missing_required_argument",
            ),
            pytest.param(
                None,
                TypeError,
                "Input must be a dictionary, got NoneType",
                id="invalid_input_object",
            ),
        ],
    )
    def test_function_call_with_invalid_input(
        self, input_data: dict[str, Any], expected_error_type: type[BaseException], expected_error_msg: str
    ) -> None:
        def add_numbers(a: int, b: int) -> str:
            return str(a + b)

        function_tool = beta_tool(add_numbers)

        with pytest.raises(expected_error_type, match=expected_error_msg):
            function_tool.call(input_data)

    def test_custom_name_and_description(self) -> None:
        def some_function(x: int) -> str:
            """Original description."""
            return str(x * 2)

        function_tool = beta_tool(some_function, name="custom_name", description="Custom description")

        assert function_tool.name == "custom_name"
        assert function_tool.description == "Custom description"

    def test_custom_input_schema_with_dict(self) -> None:
        def some_function(x: int) -> str:
            return str(x * 2)

        custom_schema: InputSchema = {
            "additionalProperties": False,
            "type": "object",
            "properties": {"x": {"type": "number", "description": "A number to double"}},
            "required": ["x"],
        }

        function_tool = beta_tool(some_function, input_schema=custom_schema)

        assert function_tool.input_schema == custom_schema

    def test_custom_input_schema_with_pydantic_model(self) -> None:
        class WeatherInput(BaseModel):
            location: str
            unit: str = "celsius"

        def get_weather(location: str, unit: str = "celsius") -> str:  # noqa: ARG001
            return f"Weather in {location}"

        # Pass the Pydantic model class directly as input_schema
        function_tool = beta_tool(get_weather, input_schema=WeatherInput)

        # Pydantic model schemas include additional metadata
        schema = function_tool.input_schema

        assert schema == {
            "title": "WeatherInput",
            "type": "object",
            "properties": {
                "location": {"title": "Location", "type": "string"},
                "unit": {"title": "Unit", "type": "string", "default": "celsius"},
            },
            "required": ["location"],
        }

    def test_to_dict_method(self) -> None:
        def simple_func(message: str) -> str:
            """A simple function."""
            return message

        function_tool = beta_tool(simple_func)
        tool_param = function_tool.to_dict()

        assert tool_param == {
            "name": "simple_func",
            "description": "A simple function.",
            "input_schema": {
                "additionalProperties": False,
                "type": "object",
                "properties": {"message": {"title": "Message", "type": "string"}},
                "required": ["message"],
            },
        }

    def test_function_without_docstring(self) -> None:
        def no_docs(x: int) -> str:  # noqa: ARG001
            return ""

        function_tool = beta_tool(no_docs)

        assert function_tool.description == ""

    def test_function_without_type_hints(self) -> None:
        def no_types(x, y=10):  # pyright: ignore[reportUnknownParameterType, reportMissingParameterType]
            return x + y  # pyright: ignore[reportUnknownVariableType]

        function_tool = beta_tool(no_types)  # type: ignore

        # Should still create a schema, though less precise (uses Any type)
        assert function_tool.input_schema == {
            "additionalProperties": False,
            "type": "object",
            "properties": {
                "x": {"title": "X"},  # Any type gets title but no type
                "y": {"title": "Y", "default": 10},
            },
            "required": ["x"],
        }

    @pytest.mark.parametrize(
        "docstring",
        [
            pytest.param(
                (
                    """Get detailed weather information for a location.

                        This function retrieves current weather conditions and optionally
                        includes a forecast for the specified location.

                        Args:
                            location: The city or location to get weather for.
                            unit: Temperature unit, either 'celsius' or 'fahrenheit'.
                            include_forecast: Whether to include forecast data.

                        Returns:
                            Weather information as a formatted string

                        Examples:
                            >>> get_weather_detailed("London")
                            "London: 15°C, partly cloudy"

                            >>> get_weather_detailed("New York", "fahrenheit", True)
                            "New York: 59°F, sunny. Tomorrow: 62°F, cloudy"
                        """
                ),
                id="google_style_docstring",
            ),
            pytest.param(
                (
                    """Get detailed weather information for a location.

                        This function retrieves current weather conditions and optionally
                        includes a forecast for the specified location.

                        :param location: The city or location to get weather for.
                        :type location: str
                        :param unit: Temperature unit, either 'celsius' or 'fahrenheit'.
                        :type unit: str
                        :param include_forecast: Whether to include forecast data.
                        :type include_forecast: bool

                        :returns: Weather information as a formatted string.
                        :rtype: str

                        :example:
                            >>> get_weather_detailed("London")
                            "London: 15°C, partly cloudy"

                            >>> get_weather_detailed("New York", "fahrenheit", True)
                            "New York: 59°F, sunny. Tomorrow: 62°F, cloudy
                        """
                ),
                id="rest_style_docstring",
            ),
            pytest.param(
                (
                    """Get detailed weather information for a location.

                        This function retrieves current weather conditions and optionally
                        includes a forecast for the specified location.

                        Parameters
                        ----------
                        location : str
                            The city or location to get weather for.
                        unit : str
                            Temperature unit, either 'celsius' or 'fahrenheit'.
                        include_forecast : bool
                            Whether to include forecast data.

                        Returns
                        -------
                        str
                            Weather information as a formatted string.

                        Examples
                        --------
                        >>> get_weather_detailed("London")
                        "London: 15°C, partly cloudy"

                        >>> get_weather_detailed("New York", "fahrenheit", True)
                        "New York: 59°F, sunny. Tomorrow: 62°F, cloudy"
                        """
                ),
                id="numpy_style_docstring",
            ),
            pytest.param(
                (
                    """Get detailed weather information for a location.

                        This function retrieves current weather conditions and optionally
                        includes a forecast for the specified location.

                        @param location: The city or location to get weather for.
                        @type location: str
                        @param unit: Temperature unit, either 'celsius' or 'fahrenheit'.
                        @type unit: str
                        @param include_forecast: Whether to include forecast data.
                        @type include_forecast: bool

                        @return: Weather information as a formatted string.
                        @rtype: str

                        @example:
                            >>> get_weather_detailed("London")
                            "London: 15°C, partly cloudy"

                            >>> get_weather_detailed("New York", "fahrenheit", True)
                            "New York: 59°F, sunny. Tomorrow: 62°F, cloudy"
                        """
                ),
                id="epydoc_style_docstring",
            ),
        ],
    )
    def test_docstring_parsing_with_parameters(self, docstring: str) -> None:
        def get_weather_detailed(location: str, unit: str = "celsius", include_forecast: bool = False) -> str:  # noqa: ARG001
            return f"Weather for {location}"

        get_weather_detailed.__doc__ = docstring

        function_tool = beta_tool(get_weather_detailed)

        expected_description = (
            "Get detailed weather information for a location.\n\n"
            "This function retrieves current weather conditions and optionally\n"
            "includes a forecast for the specified location."
        )
        expected_schema = {
            "additionalProperties": False,
            "type": "object",
            "properties": {
                "location": {
                    "title": "Location",
                    "type": "string",
                    "description": "The city or location to get weather for.",
                },
                "unit": {
                    "title": "Unit",
                    "type": "string",
                    "default": "celsius",
                    "description": "Temperature unit, either 'celsius' or 'fahrenheit'.",
                },
                "include_forecast": {
                    "title": "Include Forecast",
                    "type": "boolean",
                    "default": False,
                    "description": "Whether to include forecast data.",
                },
            },
            "required": ["location"],
        }
        assert function_tool.description == expected_description
        assert function_tool.input_schema == expected_schema

    def test_decorator_without_parentheses(self) -> None:
        """Test using @function_tool decorator without parentheses."""

        @beta_tool
        def multiply(x: int, y: int) -> str:
            """Multiply two numbers."""
            return str(x * y)

        assert multiply.name == "multiply"
        assert multiply.description == "Multiply two numbers."
        assert multiply.call({"x": 3, "y": 4}) == "12"

        expected_schema = {
            "additionalProperties": False,
            "type": "object",
            "properties": {
                "x": {"title": "X", "type": "integer"},
                "y": {"title": "Y", "type": "integer"},
            },
            "required": ["x", "y"],
        }
        assert multiply.input_schema == expected_schema

    def test_decorator_with_parentheses(self) -> None:
        """Test using @function_tool() decorator with parentheses."""

        @beta_tool()
        def divide(a: float, b: float) -> str:
            """Divide two numbers."""
            return str(a / b)

        assert divide.name == "divide"
        assert divide.description == "Divide two numbers."
        assert divide.call({"a": 10.0, "b": 2.0}) == "5.0"

    def test_decorator_with_custom_parameters(self) -> None:
        """Test using @function_tool() decorator with custom name and description."""

        @beta_tool(name="custom_calculator", description="A custom calculator function")
        def calculate(value: int) -> str:
            """Original description that should be overridden."""
            return str(value * 2)

        assert calculate.name == "custom_calculator"
        assert calculate.description == "A custom calculator function"
        assert calculate.call({"value": 5}) == "10"

    def test_docstring_parsing_simple(self) -> None:
        """Test that simple docstrings still work correctly."""

        def simple_add(a: int, b: int) -> str:
            """Add two numbers together."""
            return str(a + b)

        function_tool = beta_tool(simple_add)

        assert function_tool.description == "Add two numbers together."
        assert _get_parameters_info(function_tool) == {}

        # Schema should not have descriptions for parameters
        expected_schema = {
            "additionalProperties": False,
            "type": "object",
            "properties": {"a": {"title": "A", "type": "integer"}, "b": {"title": "B", "type": "integer"}},
            "required": ["a", "b"],
        }

        assert function_tool.input_schema == expected_schema


def _get_parameters_info(fn: BaseFunctionTool[Any]) -> dict[str, str]:
    param_info: dict[str, str] = {}
    for param in fn._parsed_docstring.params:
        if param.description:
            param_info[param.arg_name] = param.description.strip()
    return param_info