Skip to content

Commit

Permalink
changes
Browse files Browse the repository at this point in the history
  • Loading branch information
abidlabs committed Feb 26, 2025
1 parent 8c97900 commit 3a82d99
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
16 changes: 16 additions & 0 deletions groovy/transpiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -621,9 +621,25 @@ def _is_valid_gradio_return(node: ast.AST) -> bool:
if isinstance(node.func, ast.Attribute) and isinstance(node.func.value, ast.Name):
# Check for gr.Component or gradio.Component
if node.func.value.id in {"gr", "gradio"}:
# Check that there are no positional arguments
if node.args:
return False

# Check that 'value' is not in the keyword arguments
for kw in node.keywords:
if kw.arg == "value":
return False
return True
elif isinstance(node.func, ast.Name):
# Check for direct Component call if imported
# Check that there are no positional arguments
if node.args:
return False

# Check that 'value' is not in the keyword arguments
for kw in node.keywords:
if kw.arg == "value":
return False
return True

# Check for tuple or list of Gradio components
Expand Down
44 changes: 43 additions & 1 deletion tests/test_transpiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,4 +273,46 @@ def multiple_components():
expected = """function multiple_components() {
return [{"placeholder": "Component 1", "__type__": "update"}, {"variant": "primary", "__type__": "update"}];
}"""
assert result.strip() == expected.strip()
assert result.strip() == expected.strip()


def test_validate_no_value_param():
def valid_component():
return gradio.Textbox(placeholder="This is valid")

# This should pass validation
result = transpile(valid_component, validate=True)
expected = """function valid_component() {
return {"placeholder": "This is valid", "__type__": "update"};
}"""
assert result.strip() == expected.strip()


def test_validate_with_value_param():
def invalid_component():
return gradio.Textbox(value="This is invalid")

with pytest.raises(TranspilerError) as e:
transpile(invalid_component, validate=True)

assert "Function must only return Gradio component updates" in str(e.value)


def test_validate_with_positional_args():
def invalid_positional():
return gradio.Textbox("This is invalid")

with pytest.raises(TranspilerError) as e:
transpile(invalid_positional, validate=True)

assert "Function must only return Gradio component updates" in str(e.value)


def test_validate_mixed_valid_invalid_components():
def mixed_components():
return gradio.Textbox(placeholder="Valid"), gradio.Button(value="Invalid")

with pytest.raises(TranspilerError) as e:
transpile(mixed_components, validate=True)

assert "Function must only return Gradio component updates" in str(e.value)

0 comments on commit 3a82d99

Please sign in to comment.