Spaces:
Running
Running
Jeremiah Lowin commited on
Commit ·
67d0f98
1
Parent(s): a537180
Add tests
Browse files- tests/utilities/test_types.py +103 -1
tests/utilities/test_types.py
CHANGED
|
@@ -2,7 +2,12 @@ from typing import Annotated, Any
|
|
| 2 |
|
| 3 |
import pytest
|
| 4 |
|
| 5 |
-
from fastmcp.utilities.types import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
|
| 8 |
class BaseClass:
|
|
@@ -143,3 +148,100 @@ class TestImage:
|
|
| 143 |
ValueError, match="Only one of path or data can be provided"
|
| 144 |
):
|
| 145 |
Image(path="test.png", data=b"test")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
import pytest
|
| 4 |
|
| 5 |
+
from fastmcp.utilities.types import (
|
| 6 |
+
Image,
|
| 7 |
+
find_kwarg_by_type,
|
| 8 |
+
is_class_member_of_type,
|
| 9 |
+
issubclass_safe,
|
| 10 |
+
)
|
| 11 |
|
| 12 |
|
| 13 |
class BaseClass:
|
|
|
|
| 148 |
ValueError, match="Only one of path or data can be provided"
|
| 149 |
):
|
| 150 |
Image(path="test.png", data=b"test")
|
| 151 |
+
|
| 152 |
+
|
| 153 |
+
class TestFindKwargByType:
|
| 154 |
+
def test_exact_type_match(self):
|
| 155 |
+
"""Test finding parameter with exact type match."""
|
| 156 |
+
|
| 157 |
+
def func(a: int, b: str, c: BaseClass):
|
| 158 |
+
pass
|
| 159 |
+
|
| 160 |
+
assert find_kwarg_by_type(func, BaseClass) == "c"
|
| 161 |
+
|
| 162 |
+
def test_no_matching_parameter(self):
|
| 163 |
+
"""Test finding parameter when no match exists."""
|
| 164 |
+
|
| 165 |
+
def func(a: int, b: str, c: OtherClass):
|
| 166 |
+
pass
|
| 167 |
+
|
| 168 |
+
assert find_kwarg_by_type(func, BaseClass) is None
|
| 169 |
+
|
| 170 |
+
def test_parameter_with_no_annotation(self):
|
| 171 |
+
"""Test with a parameter that has no type annotation."""
|
| 172 |
+
|
| 173 |
+
def func(a: int, b, c: BaseClass):
|
| 174 |
+
pass
|
| 175 |
+
|
| 176 |
+
assert find_kwarg_by_type(func, BaseClass) == "c"
|
| 177 |
+
|
| 178 |
+
def test_union_type_match_pipe_syntax(self):
|
| 179 |
+
"""Test finding parameter with union type using pipe syntax."""
|
| 180 |
+
|
| 181 |
+
def func(a: int, b: str | BaseClass, c: str):
|
| 182 |
+
pass
|
| 183 |
+
|
| 184 |
+
assert find_kwarg_by_type(func, BaseClass) == "b"
|
| 185 |
+
|
| 186 |
+
def test_union_type_match_typing_union(self):
|
| 187 |
+
"""Test finding parameter with union type using Union."""
|
| 188 |
+
|
| 189 |
+
def func(a: int, b: str | BaseClass, c: str):
|
| 190 |
+
pass
|
| 191 |
+
|
| 192 |
+
assert find_kwarg_by_type(func, BaseClass) == "b"
|
| 193 |
+
|
| 194 |
+
def test_annotated_type_match(self):
|
| 195 |
+
"""Test finding parameter with Annotated type."""
|
| 196 |
+
|
| 197 |
+
def func(a: int, b: Annotated[BaseClass, "metadata"], c: str):
|
| 198 |
+
pass
|
| 199 |
+
|
| 200 |
+
assert find_kwarg_by_type(func, BaseClass) == "b"
|
| 201 |
+
|
| 202 |
+
def test_method_parameter(self):
|
| 203 |
+
"""Test finding parameter in a class method."""
|
| 204 |
+
|
| 205 |
+
class TestClass:
|
| 206 |
+
def method(self, a: int, b: BaseClass):
|
| 207 |
+
pass
|
| 208 |
+
|
| 209 |
+
instance = TestClass()
|
| 210 |
+
assert find_kwarg_by_type(instance.method, BaseClass) == "b"
|
| 211 |
+
|
| 212 |
+
def test_static_method_parameter(self):
|
| 213 |
+
"""Test finding parameter in a static method."""
|
| 214 |
+
|
| 215 |
+
class TestClass:
|
| 216 |
+
@staticmethod
|
| 217 |
+
def static_method(a: int, b: BaseClass, c: str):
|
| 218 |
+
pass
|
| 219 |
+
|
| 220 |
+
assert find_kwarg_by_type(TestClass.static_method, BaseClass) == "b"
|
| 221 |
+
|
| 222 |
+
def test_class_method_parameter(self):
|
| 223 |
+
"""Test finding parameter in a class method."""
|
| 224 |
+
|
| 225 |
+
class TestClass:
|
| 226 |
+
@classmethod
|
| 227 |
+
def class_method(cls, a: int, b: BaseClass, c: str):
|
| 228 |
+
pass
|
| 229 |
+
|
| 230 |
+
assert find_kwarg_by_type(TestClass.class_method, BaseClass) == "b"
|
| 231 |
+
|
| 232 |
+
def test_multiple_matching_parameters(self):
|
| 233 |
+
"""Test finding first parameter when multiple matches exist."""
|
| 234 |
+
|
| 235 |
+
def func(a: BaseClass, b: str, c: BaseClass):
|
| 236 |
+
pass
|
| 237 |
+
|
| 238 |
+
# Should return the first match
|
| 239 |
+
assert find_kwarg_by_type(func, BaseClass) == "a"
|
| 240 |
+
|
| 241 |
+
def test_subclass_match(self):
|
| 242 |
+
"""Test finding parameter with a subclass of the target type."""
|
| 243 |
+
|
| 244 |
+
def func(a: int, b: ChildClass, c: str):
|
| 245 |
+
pass
|
| 246 |
+
|
| 247 |
+
assert find_kwarg_by_type(func, BaseClass) == "b"
|