diff --git a/CHANGELOG.md b/CHANGELOG.md index 6663bd209a..3b56b018e5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ - Support using an empty list as `gr.Dataframe` value, by [@space-nuko](https://github.com/space-nuko) in [PR 3646](https://github.com/gradio-app/gradio/pull/3646) - Correct the documentation of `gr.File` component to state that its preprocessing method converts the uploaded file to a temporary file, by @RussellLuo in [PR 3660](https://github.com/gradio-app/gradio/pull/3660) - Fixed bug in Serializer ValueError text by [@osanseviero](https://github.com/osanseviero) in [PR 3669](https://github.com/gradio-app/gradio/pull/3669) +- Fix default parameter argument and `gr.Progress` used in same function, by [@space-nuko](https://github.com/space-nuko) in [PR 3671](https://github.com/gradio-app/gradio/pull/3671) - Hide `Remove All` button in `gr.Dropdown` single-select mode by [@space-nuko](https://github.com/space-nuko) in [PR 3678](https://github.com/gradio-app/gradio/pull/3678) ## Documentation Changes: diff --git a/gradio/helpers.py b/gradio/helpers.py index 493a7fe8db..4f254eb82c 100644 --- a/gradio/helpers.py +++ b/gradio/helpers.py @@ -630,6 +630,9 @@ def special_args( event_data_index = i if inputs is not None and event_data is not None: inputs.insert(i, param.annotation(event_data.target, event_data._data)) + elif param.default is not param.empty: + if inputs is not None and len(inputs) <= i: + inputs.insert(i, param.default) if inputs is not None: while len(inputs) < len(positional_args): i = len(inputs) diff --git a/test/test_blocks.py b/test/test_blocks.py index c0bd486ddb..77578c283e 100644 --- a/test/test_blocks.py +++ b/test/test_blocks.py @@ -25,6 +25,7 @@ from gradio.exceptions import DuplicateBlockError from gradio.networking import Server, get_first_available_port from gradio.test_data.blocks_configs import XRAY_CONFIG from gradio.utils import assert_configs_are_equivalent_besides_ids +from gradio.events import SelectData pytest_plugins = ("pytest_asyncio",) @@ -1412,6 +1413,115 @@ class TestAddRequests: inputs_ = gr.helpers.special_args(moo, copy.deepcopy(inputs), request)[0] assert inputs_ == [request] + inputs + [request] + def test_default_args(self): + def moo(a, b, c=42): + return a + b + c + + inputs = [1, 2] + request = gr.Request() + inputs_ = gr.helpers.special_args(moo, copy.deepcopy(inputs), request)[0] + assert inputs_ == inputs + [42] + + inputs = [1, 2, 24] + request = gr.Request() + inputs_ = gr.helpers.special_args(moo, copy.deepcopy(inputs), request)[0] + assert inputs_ == inputs + + def test_default_args_with_progress(self): + pr = gr.Progress() + + def moo(a, b, c=42, pr=pr): + return a + b + c + + inputs = [1, 2] + request = gr.Request() + inputs_, progress_index, _ = gr.helpers.special_args( + moo, copy.deepcopy(inputs), request + ) + assert inputs_ == inputs + [42, pr] + assert progress_index == 3 + + inputs = [1, 2, 24] + request = gr.Request() + inputs_, progress_index, _ = gr.helpers.special_args( + moo, copy.deepcopy(inputs), request + ) + assert inputs_ == inputs + [pr] + assert progress_index == 3 + + def moo(a, b, pr=pr, c=42): + return a + b + c + + inputs = [1, 2] + request = gr.Request() + inputs_, progress_index, _ = gr.helpers.special_args( + moo, copy.deepcopy(inputs), request + ) + assert inputs_ == inputs + [pr, 42] + assert progress_index == 2 + + def test_default_args_with_request(self): + pr = gr.Progress() + + def moo(a, b, req: gr.Request, c=42): + return a + b + c + + inputs = [1, 2] + request = gr.Request() + inputs_ = gr.helpers.special_args( + moo, copy.deepcopy(inputs), request + )[0] + assert inputs_ == inputs + [request, 42] + + def moo(a, b, req: gr.Request, c=42, pr=pr): + return a + b + c + + inputs = [1, 2] + request = gr.Request() + inputs_, progress_index, _ = gr.helpers.special_args( + moo, copy.deepcopy(inputs), request + ) + assert inputs_ == inputs + [request, 42, pr] + assert progress_index == 4 + + def test_default_args_with_event_data(self): + pr = gr.Progress() + target = gr.Textbox() + + def moo(a, b, ed: SelectData, c=42): + return a + b + c + + event_data = SelectData(target=target, data={"index": 24, "value": "foo"}) + inputs = [1, 2] + request = gr.Request() + inputs_ = gr.helpers.special_args( + moo, copy.deepcopy(inputs), request, event_data + )[0] + assert len(inputs_) == 4 + new_event_data = inputs_[2] + assert inputs_ == inputs + [new_event_data, 42] + assert isinstance(new_event_data, SelectData) + assert new_event_data.target == target + assert new_event_data.index == 24 + assert new_event_data.value == "foo" + + def moo(a, b, ed: SelectData, c=42, pr=pr): + return a + b + c + + inputs = [1, 2] + request = gr.Request() + inputs_, progress_index, _ = gr.helpers.special_args( + moo, copy.deepcopy(inputs), request, event_data + ) + assert len(inputs_) == 5 + new_event_data = inputs_[2] + assert inputs_ == inputs + [new_event_data, 42, pr] + assert progress_index == 4 + assert isinstance(new_event_data, SelectData) + assert new_event_data.target == target + assert new_event_data.index == 24 + assert new_event_data.value == "foo" + def test_queue_enabled_for_fn(): with gr.Blocks() as demo: