Skip to content

test(function_schema): add tests for enforce_type_annotations behavior #1109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions tests/test_function_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,3 +451,37 @@ def foo(x: int) -> int:

assert fs.name == "custom"
assert fs.params_json_schema.get("title") == "custom_args"


def test_unannotated_function_with_enforcement():
"""Test enforcement raises error for unannotated parameters"""
def my_func(x): ...

with pytest.raises(ValueError) as exc_info:
function_schema(my_func, enforce_type_annotations=True)

assert "Parameter 'x' must be type-annotated" in str(exc_info.value)


def test_unannotated_function_without_enforcement():
"""Test fallback to Any when enforcement is off"""
def my_func(x): ...

schema = function_schema(my_func, enforce_type_annotations=False)
assert schema.params_json_schema["properties"]["x"]["type"] == "any"


def test_annotated_function_works_with_enforcement():
"""Test basic type hints work with enforcement"""
def my_func(x: int): ...

schema = function_schema(my_func, enforce_type_annotations=True)
assert schema.params_json_schema["properties"]["x"]["type"] == "integer"


def test_positional_args_backward_compatibility():
"""Ensure old positional args still work"""
def my_func(x: int): ...

schema = function_schema(my_func, "sphinx", enforce_type_annotations=False)
assert schema.name == "my_func"
Loading