* Fixes #9742

Validator was not handling situation where function does not return anything.

Signed-off-by: Richard Decal <public@richarddecal.com>

* add test case for function which doesn't return anything

Signed-off-by: Richard Decal <public@richarddecal.com>

* format

* add changeset

---------

Signed-off-by: Richard Decal <public@richarddecal.com>
Co-authored-by: Abubakar Abid <abubakar@huggingface.co>
Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
This commit is contained in:
Richard Decal 2024-10-18 18:48:38 -04:00 committed by GitHub
parent 80c1c664c9
commit 16895e8628
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 30 additions and 0 deletions

View File

@ -0,0 +1,5 @@
---
"gradio": patch
---
fix:Fixes #9742

View File

@ -1744,6 +1744,10 @@ Received inputs:
[{received}]"""
)
else:
if len(predictions) == 1 and predictions[0] is None:
# do not throw error if the function did not return anything
# https://github.com/gradio-app/gradio/issues/9742
return
warnings.warn(
f"""A function{name} returned too many output values (needed: {len(dep_outputs)}, returned: {len(predictions)}). Ignoring extra values.
Output components:

View File

@ -8,6 +8,7 @@ import random
import sys
import time
import uuid
import warnings
from concurrent.futures import wait
from contextlib import contextmanager
from functools import partial
@ -703,6 +704,26 @@ class TestBlocksPostprocessing:
demo.fns[0], predictions=["test", "test2", "test3"], state=None
)
@pytest.mark.asyncio
async def test_no_warning_if_func_has_no_outputs(self):
"""
Ensures that if a function has no outputs, no warning is raised.
"""
with gr.Blocks() as demo:
button = gr.Button()
def no_return():
pass
button.click(
no_return,
inputs=None,
outputs=None,
)
with warnings.catch_warnings():
warnings.simplefilter("error")
await demo.postprocess_data(demo.fns[0], predictions=None, state=None) # type: ignore
@pytest.mark.asyncio
async def test_error_raised_if_num_outputs_mismatch_with_function_name(self):
def infer(x):