Skip to content

Commit

Permalink
add test and fix
Browse files Browse the repository at this point in the history
  • Loading branch information
h-mayorquin committed Dec 13, 2024
1 parent a90f295 commit 508c8ea
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/neuroconv/utils/json_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ def get_json_schema_from_method_signature(method: Callable, exclude: Optional[li
additional_properties = True
continue

annotation = parameter.annotation
# If no annotation is provided (inspect._empty), use Any instead
annotation = Any if parameter.annotation is inspect._empty else parameter.annotation

# Pydantic uses ellipsis for required
pydantic_default = ... if parameter.default is inspect._empty else parameter.default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -424,3 +424,26 @@ def test_class_method(self, integer: int):
}

assert test_json_schema == expected_json_schema


def test_get_json_schema_from_method_signature_unannotated():
"""Test that parameters without type annotations are handled correctly by defaulting to Any."""

def method_with_unannotated_params(
annotated_param: int, unannotated_param, unannotated_with_default="default_value"
):
pass

test_json_schema = get_json_schema_from_method_signature(method=method_with_unannotated_params)
expected_json_schema = {
"additionalProperties": False,
"properties": {
"annotated_param": {"type": "integer"},
"unannotated_param": {}, # Any type in JSON Schema is represented by an empty object
"unannotated_with_default": {"default": "default_value"},
},
"required": ["annotated_param", "unannotated_param"],
"type": "object",
}

assert test_json_schema == expected_json_schema

0 comments on commit 508c8ea

Please sign in to comment.