Skip to content

Commit

Permalink
Clearer error message in Dropdown's and Radio's preprocess function (#…
Browse files Browse the repository at this point in the history
…9921)

* Clearer error message in Dropdown's and Radio's preprocess function

* add changeset

---------

Co-authored-by: gradio-pr-bot <[email protected]>
  • Loading branch information
muhammadyaseen and gradio-pr-bot authored Nov 11, 2024
1 parent 66375ac commit a70ba5e
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/spicy-cameras-pay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"gradio": minor
---

feat:Clearer error message in Dropdown's and Radio's preprocess function
2 changes: 1 addition & 1 deletion gradio/components/dropdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def preprocess(
for value in payload:
if value not in choice_values:
raise Error(
f"Value: {value} is not in the list of choices: {choice_values}"
f"Value: {value!r} (type: {type(value)}) is not in the list of choices: {choice_values}"
)
elif payload not in choice_values:
raise Error(
Expand Down
2 changes: 1 addition & 1 deletion gradio/components/radio.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def preprocess(self, payload: str | int | float | None) -> str | int | float | N
choice_values = [value for _, value in self.choices]
if payload not in choice_values:
raise Error(
f"Value: {payload} is not in the list of choices: {choice_values}"
f"Value: {payload!r} (type: {type(payload)}) is not in the list of choices: {choice_values}"
)

if self.type == "value":
Expand Down
9 changes: 9 additions & 0 deletions test/components/test_dropdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ def test_component_functions(self):
assert dropdown_input.preprocess("c full") == "c full"
assert dropdown_input.postprocess("c full") == ["c full"]

# Check that the error message clearly indicates the error source in cases where data
# representation could be ambiguous e.g. "1" (str) vs 1 (int)
dropdown_input = gr.Dropdown([1, 2, 3])
# Since pytest.raises takes a regular expression in the `match` argument, we need to escape brackets
# that have special meaning in regular expressions
expected_error_message = r"Value: '1' \(type: <class 'str'>\) is not in the list of choices: \[1, 2, 3\]"
with pytest.raises(gr.Error, match=expected_error_message):
dropdown_input.preprocess(["1", "2", "3"])

# When an external Gradio app is loaded with gr.load, the tuples are converted to lists,
# so we test that case as well
dropdown = gr.Dropdown(["a", "b", ["c", "c full"]]) # type: ignore
Expand Down
10 changes: 10 additions & 0 deletions test/components/test_radio.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ def test_component_functions(self):
radio_input = gr.Radio(["a", "b", "c"])
assert radio_input.preprocess("c") == "c"
assert radio_input.postprocess("a") == "a"

# Check that the error message clearly indicates the error source in cases where data
# representation could be ambiguous e.g. "1" (str) vs 1 (int)
radio_input = gr.Radio([1, 2, 3])
# Since pytest.raises takes a regular expression in the `match` argument, we need to escape brackets
# that have special meaning in regular expressions
expected_error_message = r"Value: '1' \(type: <class 'str'>\) is not in the list of choices: \[1, 2, 3\]"
with pytest.raises(gr.Error, match=expected_error_message):
radio_input.preprocess("1")

radio_input = gr.Radio(
choices=["a", "b", "c"], value="a", label="Pick Your One Input"
)
Expand Down

0 comments on commit a70ba5e

Please sign in to comment.