Skip to content
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,10 @@ def gather_function_parameters(self, context: FunctionInvocationContext) -> dict
continue
if param.name in context.arguments:
value: Any = context.arguments[param.name]
accepts_none = not param.is_required and param.default_value is None
if (
param.type_
not (value is None and accepts_none)
and param.type_
and "," not in param.type_
and param.type_object
and param.type_object is not inspect._empty
Expand Down
33 changes: 33 additions & 0 deletions python/tests/unit/functions/test_kernel_function_from_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,39 @@ async def override_stream(stream):
]


async def test_explicit_none_for_optional_parameters_is_not_coerced(kernel: Kernel):
@kernel_function
def func_optional(
count: int | None = None,
label: Annotated[str | None, "A label"] = None,
item: CustomTypeDataclass | None = None,
) -> Any:
return (count, label, item)

func = kernel.add_function(plugin_name="test", function=func_optional)

res = await kernel.invoke(func, arguments=KernelArguments(count=None, label=None, item=None))
assert res.value == (None, None, None)

res = await kernel.invoke(func, arguments=KernelArguments(count="3", label="x"))
assert res.value == (3, "x", None)


async def test_explicit_none_for_non_optional_parameters_raises(kernel: Kernel):
@kernel_function
def func_required(count: int) -> int:
return count

@kernel_function
def func_int_default(count: int = 5) -> int:
return count

for method in (func_required, func_int_default):
func = KernelFunction.from_method(method, "test")
with pytest.raises(FunctionExecutionException, match="Parameter count is expected to be parsed"):
await func.invoke(kernel=kernel, arguments=KernelArguments(count=None))


async def test_default_handling(kernel: Kernel):
@kernel_function
def func_default(input: str = "test"):
Expand Down
Loading