Better error messages (#2543)

* events fix

* changelog, format, better error msg

* changelog

* changelog

* mix errors

* formatting

* switched from error to warnings

* typo
This commit is contained in:
Abubakar Abid 2022-10-26 16:10:26 -07:00 committed by GitHub
parent 099e1e84ec
commit 5c80b544ab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 0 deletions

View File

@ -77,6 +77,9 @@ No changes to highlight.
* Gradio now supports batched functions by [@abidlabs](https://github.com/abidlabs) in [PR 2218](https://github.com/gradio-app/gradio/pull/2218) * Gradio now supports batched functions by [@abidlabs](https://github.com/abidlabs) in [PR 2218](https://github.com/gradio-app/gradio/pull/2218)
* Add `upload` event for `Video`, `Audio`, `Image`, and `File` components [@dawoodkhan82](https://github.com/dawoodkhan82) in [PR 2448](https://github.com/gradio-app/gradio/pull/2456) * Add `upload` event for `Video`, `Audio`, `Image`, and `File` components [@dawoodkhan82](https://github.com/dawoodkhan82) in [PR 2448](https://github.com/gradio-app/gradio/pull/2456)
* Changes websocket path for Spaces as it is no longer necessary to have a different URL for websocket connections on Spaces by [@abidlabs](https://github.com/abidlabs) in [PR 2528](https://github.com/gradio-app/gradio/pull/2528) * Changes websocket path for Spaces as it is no longer necessary to have a different URL for websocket connections on Spaces by [@abidlabs](https://github.com/abidlabs) in [PR 2528](https://github.com/gradio-app/gradio/pull/2528)
* Clearer error message when events are defined outside of a Blocks scope, and a warning if you
try to use `Series` or `Parallel` with `Blocks` by [@abidlabs](https://github.com/abidlabs) in [PR 2543](https://github.com/gradio-app/gradio/pull/2543)
## Contributors Shoutout: ## Contributors Shoutout:
No changes to highlight. No changes to highlight.

View File

@ -10,6 +10,7 @@ import sys
import time import time
import warnings import warnings
import webbrowser import webbrowser
from tkinter import N
from types import ModuleType from types import ModuleType
from typing import ( from typing import (
TYPE_CHECKING, TYPE_CHECKING,
@ -162,6 +163,10 @@ class Block:
inputs = [inputs] inputs = [inputs]
if not isinstance(outputs, list): if not isinstance(outputs, list):
outputs = [outputs] outputs = [outputs]
if Context.root_block is None:
raise AttributeError(
f"{event_name}() and other events can only be called within a Blocks context."
)
Context.root_block.fns.append(BlockFunction(fn, preprocess, postprocess)) Context.root_block.fns.append(BlockFunction(fn, preprocess, postprocess))
if api_name is not None: if api_name is not None:
api_name_ = utils.append_unique_suffix( api_name_ = utils.append_unique_suffix(

View File

@ -2,6 +2,7 @@
Ways to transform interfaces to produce new interfaces Ways to transform interfaces to produce new interfaces
""" """
import asyncio import asyncio
import warnings
from typing import TYPE_CHECKING, List from typing import TYPE_CHECKING, List
import gradio import gradio
@ -34,6 +35,11 @@ class Parallel(gradio.Interface):
outputs: List[IOComponent] = [] outputs: List[IOComponent] = []
for interface in interfaces: for interface in interfaces:
if not (isinstance(interface, gradio.Interface)):
warnings.warn(
"Parallel requires all inputs to be of type Interface. "
"May not work as expected."
)
outputs.extend(interface.output_components) outputs.extend(interface.output_components)
async def parallel_fn(*args): async def parallel_fn(*args):
@ -108,6 +114,12 @@ class Series(gradio.Interface):
return data[0] return data[0]
return data return data
for interface in interfaces:
if not (isinstance(interface, gradio.Interface)):
warnings.warn(
"Series requires all inputs to be of type Interface. May "
"not work as expected."
)
connected_fn.__name__ = " => ".join([io.__name__ for io in interfaces]) connected_fn.__name__ = " => ".join([io.__name__ for io in interfaces])
kwargs = { kwargs = {

16
test/test_events.py Normal file
View File

@ -0,0 +1,16 @@
import pytest
import gradio as gr
class TestEventErrors:
def test_event_defined_invalid_scope(self):
with gr.Blocks() as demo:
textbox = gr.Textbox()
textbox.blur(lambda x: x + x, textbox, textbox)
with pytest.raises(AttributeError):
demo.load(lambda: "hello", None, textbox)
with pytest.raises(AttributeError):
textbox.change(lambda x: x + x, textbox, textbox)