Update tests, proper handling of return values
This commit is contained in:
parent
51359bf11e
commit
89c0a49ba5
|
@ -36,9 +36,12 @@ def _convert_type_hints_to_json_schema(func):
|
|||
properties = {}
|
||||
|
||||
signature = inspect.signature(func)
|
||||
required = [
|
||||
param_name for param_name, param in signature.parameters.items() if param.default == inspect.Parameter.empty
|
||||
]
|
||||
required = []
|
||||
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():
|
||||
if param_name == "return":
|
||||
|
@ -51,8 +54,7 @@ def _convert_type_hints_to_json_schema(func):
|
|||
|
||||
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):
|
||||
if (origin := get_origin(hint)) is not None:
|
||||
if origin is Union:
|
||||
|
|
|
@ -15,7 +15,15 @@ class JsonSchemaGeneratorTest(unittest.TestCase):
|
|||
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']}}
|
||||
expected_schema = {
|
||||
"name": "fn",
|
||||
"description": "Test function",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {"x": {"type": "integer", "description": "The input"}},
|
||||
"required": ["x"],
|
||||
},
|
||||
}
|
||||
self.assertEqual(schema, expected_schema)
|
||||
|
||||
def test_union(self):
|
||||
|
@ -28,7 +36,15 @@ class JsonSchemaGeneratorTest(unittest.TestCase):
|
|||
return x
|
||||
|
||||
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)
|
||||
|
||||
def test_optional(self):
|
||||
|
@ -41,7 +57,15 @@ class JsonSchemaGeneratorTest(unittest.TestCase):
|
|||
return x
|
||||
|
||||
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)
|
||||
|
||||
def test_default_arg(self):
|
||||
|
@ -54,7 +78,11 @@ class JsonSchemaGeneratorTest(unittest.TestCase):
|
|||
return x
|
||||
|
||||
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)
|
||||
|
||||
def test_nested_list(self):
|
||||
|
@ -67,7 +95,21 @@ class JsonSchemaGeneratorTest(unittest.TestCase):
|
|||
return x
|
||||
|
||||
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)
|
||||
|
||||
def test_multiple_arguments(self):
|
||||
|
@ -81,7 +123,18 @@ class JsonSchemaGeneratorTest(unittest.TestCase):
|
|||
return x
|
||||
|
||||
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)
|
||||
|
||||
def test_multiple_complex_arguments(self):
|
||||
|
@ -95,7 +148,22 @@ class JsonSchemaGeneratorTest(unittest.TestCase):
|
|||
return x
|
||||
|
||||
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)
|
||||
|
||||
def test_missing_docstring(self):
|
||||
|
@ -126,3 +194,25 @@ class JsonSchemaGeneratorTest(unittest.TestCase):
|
|||
|
||||
with self.assertRaises(ValueError):
|
||||
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)
|
||||
|
|
Loading…
Reference in New Issue