Update tests, proper handling of return values

This commit is contained in:
Matt 2024-05-08 13:54:57 +01:00
parent 51359bf11e
commit 89c0a49ba5
2 changed files with 104 additions and 12 deletions

View File

@ -36,9 +36,12 @@ def _convert_type_hints_to_json_schema(func):
properties = {} properties = {}
signature = inspect.signature(func) signature = inspect.signature(func)
required = [ required = []
param_name for param_name, param in signature.parameters.items() if param.default == inspect.Parameter.empty for param_name, param in signature.parameters.items():
] if param.annotation == inspect.Parameter.empty:
raise ValueError(f"Argument {param.name} is missing a type hint in function {func.__name__}")
if param.default == inspect.Parameter.empty:
required.append(param_name)
for param_name, param_type in type_hints.items(): for param_name, param_type in type_hints.items():
if param_name == "return": if param_name == "return":
@ -51,8 +54,7 @@ def _convert_type_hints_to_json_schema(func):
return schema return schema
# TODO: Return types!! How are those even handled? Does it even matter? I should check what the different APIs do for this
# and also add tests
def _parse_type_hint(hint): def _parse_type_hint(hint):
if (origin := get_origin(hint)) is not None: if (origin := get_origin(hint)) is not None:
if origin is Union: if origin is Union:

View File

@ -15,7 +15,15 @@ class JsonSchemaGeneratorTest(unittest.TestCase):
return x return x
schema = get_json_schema(fn) schema = get_json_schema(fn)
expected_schema = {'name': 'fn', 'description': 'Test function', 'parameters': {'type': 'object', 'properties': {'x': {'type': 'integer', 'description': 'The input'}}, 'required': ['x']}} expected_schema = {
"name": "fn",
"description": "Test function",
"parameters": {
"type": "object",
"properties": {"x": {"type": "integer", "description": "The input"}},
"required": ["x"],
},
}
self.assertEqual(schema, expected_schema) self.assertEqual(schema, expected_schema)
def test_union(self): def test_union(self):
@ -28,7 +36,15 @@ class JsonSchemaGeneratorTest(unittest.TestCase):
return x return x
schema = get_json_schema(fn) schema = get_json_schema(fn)
expected_schema = {'name': 'fn', 'description': 'Test function', 'parameters': {'type': 'object', 'properties': {'x': {'type': ['integer', 'number'], 'description': 'The input'}}, 'required': ['x']}} expected_schema = {
"name": "fn",
"description": "Test function",
"parameters": {
"type": "object",
"properties": {"x": {"type": ["integer", "number"], "description": "The input"}},
"required": ["x"],
},
}
self.assertEqual(schema, expected_schema) self.assertEqual(schema, expected_schema)
def test_optional(self): def test_optional(self):
@ -41,7 +57,15 @@ class JsonSchemaGeneratorTest(unittest.TestCase):
return x return x
schema = get_json_schema(fn) schema = get_json_schema(fn)
expected_schema = {'name': 'fn', 'description': 'Test function', 'parameters': {'type': 'object', 'properties': {'x': {'type': 'integer', 'description': 'The input', "nullable": True}}, 'required': ['x']}} expected_schema = {
"name": "fn",
"description": "Test function",
"parameters": {
"type": "object",
"properties": {"x": {"type": "integer", "description": "The input", "nullable": True}},
"required": ["x"],
},
}
self.assertEqual(schema, expected_schema) self.assertEqual(schema, expected_schema)
def test_default_arg(self): def test_default_arg(self):
@ -54,7 +78,11 @@ class JsonSchemaGeneratorTest(unittest.TestCase):
return x return x
schema = get_json_schema(fn) schema = get_json_schema(fn)
expected_schema = {'name': 'fn', 'description': 'Test function', 'parameters': {'type': 'object', 'properties': {'x': {'type': 'integer', 'description': 'The input'}}}} expected_schema = {
"name": "fn",
"description": "Test function",
"parameters": {"type": "object", "properties": {"x": {"type": "integer", "description": "The input"}}},
}
self.assertEqual(schema, expected_schema) self.assertEqual(schema, expected_schema)
def test_nested_list(self): def test_nested_list(self):
@ -67,7 +95,21 @@ class JsonSchemaGeneratorTest(unittest.TestCase):
return x return x
schema = get_json_schema(fn) schema = get_json_schema(fn)
expected_schema = {'name': 'fn', 'description': 'Test function', 'parameters': {'type': 'object', 'properties': {'x': {'type': 'array', 'items': {'type': 'array', 'items': {'type': ['integer', 'string']}}, 'description': 'The input'}}, 'required': ['x']}} expected_schema = {
"name": "fn",
"description": "Test function",
"parameters": {
"type": "object",
"properties": {
"x": {
"type": "array",
"items": {"type": "array", "items": {"type": ["integer", "string"]}},
"description": "The input",
}
},
"required": ["x"],
},
}
self.assertEqual(schema, expected_schema) self.assertEqual(schema, expected_schema)
def test_multiple_arguments(self): def test_multiple_arguments(self):
@ -81,7 +123,18 @@ class JsonSchemaGeneratorTest(unittest.TestCase):
return x return x
schema = get_json_schema(fn) schema = get_json_schema(fn)
expected_schema = {'name': 'fn', 'description': 'Test function', 'parameters': {'type': 'object', 'properties': {'x': {'type': 'integer', 'description': 'The input'}, 'y': {'type': 'string', 'description': 'Also the input'}}, 'required': ['x', 'y']}} expected_schema = {
"name": "fn",
"description": "Test function",
"parameters": {
"type": "object",
"properties": {
"x": {"type": "integer", "description": "The input"},
"y": {"type": "string", "description": "Also the input"},
},
"required": ["x", "y"],
},
}
self.assertEqual(schema, expected_schema) self.assertEqual(schema, expected_schema)
def test_multiple_complex_arguments(self): def test_multiple_complex_arguments(self):
@ -95,7 +148,22 @@ class JsonSchemaGeneratorTest(unittest.TestCase):
return x return x
schema = get_json_schema(fn) schema = get_json_schema(fn)
expected_schema = {'name': 'fn', 'description': 'Test function', 'parameters': {'type': 'object', 'properties': {'x': {'type': 'array', 'items': {'type': ['integer', 'number']}, 'description': 'The input'}, 'y': {'anyOf': [{'type': 'integer'}, {'type': 'string'}], 'nullable': True, 'description': 'Also the input'}}, 'required': ['x']}} expected_schema = {
"name": "fn",
"description": "Test function",
"parameters": {
"type": "object",
"properties": {
"x": {"type": "array", "items": {"type": ["integer", "number"]}, "description": "The input"},
"y": {
"anyOf": [{"type": "integer"}, {"type": "string"}],
"nullable": True,
"description": "Also the input",
},
},
"required": ["x"],
},
}
self.assertEqual(schema, expected_schema) self.assertEqual(schema, expected_schema)
def test_missing_docstring(self): def test_missing_docstring(self):
@ -126,3 +194,25 @@ class JsonSchemaGeneratorTest(unittest.TestCase):
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
get_json_schema(fn) get_json_schema(fn)
def test_return_value_has_no_effect(self):
# We ignore return values, so we want to make sure they don't affect the schema
def fn(x: int) -> int:
"""
Test function
:param x: The input
"""
return x
schema = get_json_schema(fn)
expected_schema = {
"name": "fn",
"description": "Test function",
"parameters": {
"type": "object",
"properties": {"x": {"type": "integer", "description": "The input"}},
"required": ["x"],
},
}
self.assertEqual(schema, expected_schema)