Fix postprocess when value is a dict with only one key (#2242)

* Unwrap + test

* Update tests + use resolve_singleton

* Undo test change
This commit is contained in:
Freddy Boulton 2022-09-14 11:06:32 -05:00 committed by GitHub
parent 5e7f5a0f6b
commit 422de63663
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 1 deletions

View File

@ -673,7 +673,7 @@ class Blocks(BlockContext):
)
output_index = dependency["outputs"].index(component._id)
reordered_predictions[output_index] = value
predictions = reordered_predictions
predictions = utils.resolve_singleton(reordered_predictions)
elif any(keys_are_blocks):
raise ValueError(
"Returned dictionary included some keys as Components. Either all keys must be Components to assign Component values, or return a List of values to assign output values in order."

View File

@ -301,6 +301,25 @@ class TestComponentsInBlocks:
assert output[0]["value"] == "NO_VALUE"
def test_blocks_returns_correct_output_dict_single_key():
with gr.Blocks() as demo:
num = gr.Number()
num2 = gr.Number()
update = gr.Button(value="update")
def update_values():
return {num2: gr.Number.update(value=42)}
update.click(update_values, inputs=[num], outputs=[num2])
output = demo.postprocess_data(0, {num2: gr.Number.update(value=42)}, state=None)
assert output[0]["value"] == 42
output = demo.postprocess_data(0, {num2: 23}, state=None)
assert output[0] == 23
class TestCallFunction:
@pytest.mark.asyncio
async def test_call_regular_function(self):