From eb7a300f84c674b10f5a6b406e7f1439a8d8753d Mon Sep 17 00:00:00 2001 From: MUHAMMAD SALMAN HUSSAIN <160324527+mshsheikh@users.noreply.github.com> Date: Mon, 14 Jul 2025 23:37:58 +0500 Subject: [PATCH 1/2] test(function_schema): add tests for enforce_type_annotations behavior Adds 4 new tests to validate enforce_type_annotations: - Enforcement: Checks if unannotated functions raise errors. - Fallback: Confirms unannotated parameters default to Any when disabled. - Compatibility: Ensures positional args (e.g., function_schema(my_func, "sphinx")) still work. See Updated Code: https://github.com/openai/openai-agents-python/blob/main/src/agents/function_schema.py --- tests/test_function_schema.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/tests/test_function_schema.py b/tests/test_function_schema.py index 54e7e2f9e..aa48a4676 100644 --- a/tests/test_function_schema.py +++ b/tests/test_function_schema.py @@ -451,3 +451,37 @@ def foo(x: int) -> int: assert fs.name == "custom" assert fs.params_json_schema.get("title") == "custom_args" + +# Tests for, "enforce_type_annotations" update inside "function_schema.py" +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" From 78f99bafd29c80850684dfe1058bc790cc6a54e6 Mon Sep 17 00:00:00 2001 From: MUHAMMAD SALMAN HUSSAIN <160324527+mshsheikh@users.noreply.github.com> Date: Tue, 15 Jul 2025 01:07:40 +0500 Subject: [PATCH 2/2] Update test_function_schema.py --- tests/test_function_schema.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/test_function_schema.py b/tests/test_function_schema.py index aa48a4676..38d6d5e67 100644 --- a/tests/test_function_schema.py +++ b/tests/test_function_schema.py @@ -452,21 +452,21 @@ def foo(x: int) -> int: assert fs.name == "custom" assert fs.params_json_schema.get("title") == "custom_args" -# Tests for, "enforce_type_annotations" update inside "function_schema.py" + 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" @@ -474,7 +474,7 @@ def my_func(x): ... 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" @@ -482,6 +482,6 @@ def my_func(x: int): ... 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"