|
| 1 | +from functools import partial |
| 2 | +from typing import Any |
| 3 | +from unittest.mock import Mock |
| 4 | + |
| 5 | +from pydantic_ai import RunContext |
| 6 | +from pydantic_ai._function_schema import _takes_ctx # type: ignore |
| 7 | + |
| 8 | + |
| 9 | +def test_regular_function_with_ctx(): |
| 10 | + """Test regular function that takes RunContext as first parameter.""" |
| 11 | + |
| 12 | + def func_with_ctx(ctx: RunContext[Any], x: int) -> str: ... # pragma: no cover |
| 13 | + |
| 14 | + assert _takes_ctx(func_with_ctx) is True |
| 15 | + |
| 16 | + |
| 17 | +def test_regular_function_without_ctx(): |
| 18 | + """Test regular function that doesn't take RunContext as first parameter.""" |
| 19 | + |
| 20 | + def func_without_ctx(x: int, y: str) -> str: ... # pragma: no cover |
| 21 | + |
| 22 | + assert _takes_ctx(func_without_ctx) is False |
| 23 | + |
| 24 | + |
| 25 | +def test_regular_function_no_params(): |
| 26 | + """Test regular function with no parameters.""" |
| 27 | + |
| 28 | + def func_no_params() -> str: ... # pragma: no cover |
| 29 | + |
| 30 | + assert _takes_ctx(func_no_params) is False |
| 31 | + |
| 32 | + |
| 33 | +def test_regular_function_ctx_not_first(): |
| 34 | + """Test regular function where RunContext is not the first parameter.""" |
| 35 | + |
| 36 | + def func_ctx_not_first(x: int, ctx: RunContext[Any]) -> str: ... # pragma: no cover |
| 37 | + |
| 38 | + assert _takes_ctx(func_ctx_not_first) is False |
| 39 | + |
| 40 | + |
| 41 | +def test_partial_function_with_ctx(): |
| 42 | + """Test partial function where original function takes RunContext as first parameter.""" |
| 43 | + |
| 44 | + def original_func(ctx: RunContext[Any], x: int, y: str) -> str: ... # pragma: no cover |
| 45 | + |
| 46 | + # Create partial with y bound |
| 47 | + partial_func = partial(original_func, y='bound') |
| 48 | + |
| 49 | + assert _takes_ctx(partial_func) is True |
| 50 | + |
| 51 | + |
| 52 | +def test_partial_function_without_ctx(): |
| 53 | + """Test partial function where original function doesn't take RunContext.""" |
| 54 | + |
| 55 | + def original_func(x: int, y: str, z: float) -> str: ... # pragma: no cover |
| 56 | + |
| 57 | + # Create partial with z bound |
| 58 | + partial_func = partial(original_func, z=3.14) |
| 59 | + |
| 60 | + assert _takes_ctx(partial_func) is False |
| 61 | + |
| 62 | + |
| 63 | +def test_partial_function_ctx_bound(): |
| 64 | + """Test partial function where RunContext parameter is bound.""" |
| 65 | + |
| 66 | + def original_func(ctx: RunContext[Any], x: int, y: str) -> str: ... # pragma: no cover |
| 67 | + |
| 68 | + mock_ctx = Mock(spec=RunContext[Any]) |
| 69 | + partial_func = partial(original_func, ctx=mock_ctx) |
| 70 | + |
| 71 | + assert _takes_ctx(partial_func) is True |
| 72 | + |
| 73 | + |
| 74 | +def test_callable_class_with_ctx(): |
| 75 | + """Test callable class where __call__ takes RunContext as first parameter.""" |
| 76 | + |
| 77 | + class CallableWithCtx: |
| 78 | + def __call__(self, ctx: RunContext[Any], x: int) -> str: ... # pragma: no cover |
| 79 | + |
| 80 | + callable_obj = CallableWithCtx() |
| 81 | + |
| 82 | + assert _takes_ctx(callable_obj) is True |
| 83 | + |
| 84 | + |
| 85 | +def test_callable_class_without_ctx(): |
| 86 | + """Test callable class where __call__ doesn't take RunContext.""" |
| 87 | + |
| 88 | + class CallableWithoutCtx: |
| 89 | + def __call__(self, x: int, y: str) -> str: ... # pragma: no cover |
| 90 | + |
| 91 | + callable_obj = CallableWithoutCtx() |
| 92 | + |
| 93 | + assert _takes_ctx(callable_obj) is False |
| 94 | + |
| 95 | + |
| 96 | +def test_callable_class_ctx_not_first(): |
| 97 | + """Test callable class where RunContext is not the first parameter.""" |
| 98 | + |
| 99 | + class CallableCtxNotFirst: |
| 100 | + def __call__(self, x: int, ctx: RunContext[Any]) -> str: ... # pragma: no cover |
| 101 | + |
| 102 | + callable_obj = CallableCtxNotFirst() |
| 103 | + |
| 104 | + assert _takes_ctx(callable_obj) is False |
| 105 | + |
| 106 | + |
| 107 | +def test_method_with_ctx(): |
| 108 | + """Test bound method that takes RunContext as first parameter (after ).""" |
| 109 | + |
| 110 | + class TestClass: |
| 111 | + def method_with_ctx(self, ctx: RunContext[Any], x: int) -> str: ... # pragma: no cover |
| 112 | + |
| 113 | + obj = TestClass() |
| 114 | + bound_method = obj.method_with_ctx |
| 115 | + |
| 116 | + assert _takes_ctx(bound_method) is True |
| 117 | + |
| 118 | + |
| 119 | +def test_method_without_ctx(): |
| 120 | + """Test bound method that doesn't take RunContext.""" |
| 121 | + |
| 122 | + class TestClass: |
| 123 | + def method_without_ctx(self, x: int, y: str) -> str: ... # pragma: no cover |
| 124 | + |
| 125 | + obj = TestClass() |
| 126 | + bound_method = obj.method_without_ctx |
| 127 | + |
| 128 | + assert _takes_ctx(bound_method) is False |
| 129 | + |
| 130 | + |
| 131 | +def test_static_method_with_ctx(): |
| 132 | + """Test static method that takes RunContext as first parameter.""" |
| 133 | + |
| 134 | + class TestClass: |
| 135 | + @staticmethod |
| 136 | + def static_method_with_ctx(ctx: RunContext[Any], x: int) -> str: ... # pragma: no cover |
| 137 | + |
| 138 | + assert _takes_ctx(TestClass.static_method_with_ctx) is True |
| 139 | + |
| 140 | + |
| 141 | +def test_static_method_without_ctx(): |
| 142 | + """Test static method that doesn't take RunContext.""" |
| 143 | + |
| 144 | + class TestClass: |
| 145 | + @staticmethod |
| 146 | + def static_method_without_ctx(x: int, y: str) -> str: ... # pragma: no cover |
| 147 | + |
| 148 | + assert _takes_ctx(TestClass.static_method_without_ctx) is False |
| 149 | + |
| 150 | + |
| 151 | +def test_class_method_with_ctx(): |
| 152 | + """Test class method that takes RunContext as first parameter (after cls).""" |
| 153 | + |
| 154 | + class TestClass: |
| 155 | + @classmethod |
| 156 | + def class_method_with_ctx(cls, ctx: RunContext[Any], x: int) -> str: ... # pragma: no cover |
| 157 | + |
| 158 | + assert _takes_ctx(TestClass.class_method_with_ctx) is True |
| 159 | + |
| 160 | + |
| 161 | +def test_class_method_without_ctx(): |
| 162 | + """Test class method that doesn't take RunContext.""" |
| 163 | + |
| 164 | + class TestClass: |
| 165 | + @classmethod |
| 166 | + def class_method_without_ctx(cls, x: int, y: str) -> str: ... # pragma: no cover |
| 167 | + |
| 168 | + assert _takes_ctx(TestClass.class_method_without_ctx) is False |
| 169 | + |
| 170 | + |
| 171 | +def test_function_no_annotations(): |
| 172 | + """Test function with no type annotations.""" |
| 173 | + |
| 174 | + def func_no_annotations(ctx, x): # type: ignore |
| 175 | + ... # pragma: no cover |
| 176 | + |
| 177 | + # Without annotations, _takes_ctx should return False |
| 178 | + assert _takes_ctx(func_no_annotations) is False # type: ignore |
| 179 | + |
| 180 | + |
| 181 | +def test_function_wrong_annotation_type(): |
| 182 | + """Test function with wrong annotation type for first parameter.""" |
| 183 | + |
| 184 | + def func_wrong_annotation(ctx: str, x: int) -> str: ... # pragma: no cover |
| 185 | + |
| 186 | + assert _takes_ctx(func_wrong_annotation) is False |
| 187 | + |
| 188 | + |
| 189 | +def test_lambda_with_ctx(): |
| 190 | + """Test lambda function that takes RunContext as first parameter.""" |
| 191 | + lambda_with_ctx = lambda ctx, x: f'{ctx.deps} {x}' # type: ignore # noqa: E731 |
| 192 | + |
| 193 | + # Lambda without annotations should return False |
| 194 | + assert _takes_ctx(lambda_with_ctx) is False # type: ignore |
| 195 | + |
| 196 | + |
| 197 | +def test_builtin_function(): |
| 198 | + """Test builtin function.""" |
| 199 | + assert _takes_ctx(len) is False |
| 200 | + assert _takes_ctx(str) is False |
| 201 | + assert _takes_ctx(int) is False |
0 commit comments