Skip to content

Commit

Permalink
Add json schema unit tests (#5970)
Browse files Browse the repository at this point in the history
* Add tests

* add changeset

* Fix tests

* api-info

* Add test

* Add test

* Add email tests

* 3.8 fix 馃檮

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
  • Loading branch information
freddyaboulton and gradio-pr-bot committed Oct 18, 2023
1 parent fdc7c0a commit 0c571c0
Show file tree
Hide file tree
Showing 6 changed files with 312 additions and 35 deletions.
6 changes: 6 additions & 0 deletions .changeset/dull-adults-study.md
@@ -0,0 +1,6 @@
---
"gradio": minor
"gradio_client": minor
---

feat:Add json schema unit tests
40 changes: 26 additions & 14 deletions client/python/gradio_client/utils.py
Expand Up @@ -542,8 +542,6 @@ class APIInfoParseError(ValueError):


def get_type(schema: dict):
if not isinstance(schema, dict):
breakpoint()
if "const" in schema:
return "const"
if "enum" in schema:
Expand All @@ -556,6 +554,10 @@ def get_type(schema: dict):
return "oneOf"
elif schema.get("anyOf"):
return "anyOf"
elif schema.get("allOf"):
return "allOf"
elif "type" not in schema:
return {}
else:
raise APIInfoParseError(f"Cannot parse type for {schema}")

Expand All @@ -574,7 +576,7 @@ def _json_schema_to_python_type(schema: Any, defs) -> str:
return "Any"
type_ = get_type(schema)
if type_ == {}:
if "json" in schema["description"]:
if "json" in schema.get("description", {}):
return "Dict[Any, Any]"
else:
return "Any"
Expand All @@ -593,14 +595,19 @@ def _json_schema_to_python_type(schema: Any, defs) -> str:
elif type_ == "boolean":
return "bool"
elif type_ == "number":
return "int | float"
return "float"
elif type_ == "array":
items = schema.get("items")
items = schema.get("items", [])
if "prefixItems" in items:
elements = ", ".join(
[_json_schema_to_python_type(i, defs) for i in items["prefixItems"]]
)
return f"Tuple[{elements}]"
elif "prefixItems" in schema:
elements = ", ".join(
[_json_schema_to_python_type(i, defs) for i in schema["prefixItems"]]
)
return f"Tuple[{elements}]"
else:
elements = _json_schema_to_python_type(items, defs)
return f"List[{elements}]"
Expand All @@ -609,22 +616,27 @@ def _json_schema_to_python_type(schema: Any, defs) -> str:
def get_desc(v):
return f" ({v.get('description')})" if v.get("description") else ""

if "additionalProperties" in schema:
return f"Dict[str, {_json_schema_to_python_type(schema['additionalProperties'], defs)}]"
props = schema.get("properties", {})

props = schema.get("properties")
des = [
f"{n}: {_json_schema_to_python_type(v, defs)}{get_desc(v)}"
for n, v in props.items()
if n != "$defs"
]

des = ", ".join(
[
f"{n}: {_json_schema_to_python_type(v, defs)}{get_desc(v)}"
for n, v in props.items()
if n != "$defs"
if "additionalProperties" in schema:
des += [
f"str, {_json_schema_to_python_type(schema['additionalProperties'], defs)}"
]
)
des = ", ".join(des)
return f"Dict({des})"
elif type_ in ["oneOf", "anyOf"]:
desc = " | ".join([_json_schema_to_python_type(i, defs) for i in schema[type_]])
return desc
elif type_ == "allOf":
data = ", ".join(_json_schema_to_python_type(i, defs) for i in schema[type_])
desc = f"All[{data}]"
return desc
else:
raise APIInfoParseError(f"Cannot parse schema {schema}")

Expand Down
24 changes: 12 additions & 12 deletions client/python/test/test_client.py
Expand Up @@ -619,7 +619,7 @@ def test_numerical_to_label_space(self):
"label": "Age",
"type": {"type": "number"},
"python_type": {
"type": "int | float",
"type": "float",
"description": "",
},
"component": "Slider",
Expand All @@ -630,7 +630,7 @@ def test_numerical_to_label_space(self):
"label": "Fare (british pounds)",
"type": {"type": "number"},
"python_type": {
"type": "int | float",
"type": "float",
"description": "",
},
"component": "Slider",
Expand Down Expand Up @@ -665,7 +665,7 @@ def test_numerical_to_label_space(self):
"label": "Age",
"type": {"type": "number"},
"python_type": {
"type": "int | float",
"type": "float",
"description": "",
},
"component": "Slider",
Expand All @@ -676,7 +676,7 @@ def test_numerical_to_label_space(self):
"label": "Fare (british pounds)",
"type": {"type": "number"},
"python_type": {
"type": "int | float",
"type": "float",
"description": "",
},
"component": "Slider",
Expand Down Expand Up @@ -711,7 +711,7 @@ def test_numerical_to_label_space(self):
"label": "Age",
"type": {"type": "number"},
"python_type": {
"type": "int | float",
"type": "float",
"description": "",
},
"component": "Slider",
Expand All @@ -722,7 +722,7 @@ def test_numerical_to_label_space(self):
"label": "Fare (british pounds)",
"type": {"type": "number"},
"python_type": {
"type": "int | float",
"type": "float",
"description": "",
},
"component": "Slider",
Expand Down Expand Up @@ -798,7 +798,7 @@ def test_fetch_fixed_version_space(self, calculator_demo):
"label": "num1",
"type": {"type": "number"},
"python_type": {
"type": "int | float",
"type": "float",
"description": "",
},
"component": "Number",
Expand All @@ -822,7 +822,7 @@ def test_fetch_fixed_version_space(self, calculator_demo):
"label": "num2",
"type": {"type": "number"},
"python_type": {
"type": "int | float",
"type": "float",
"description": "",
},
"component": "Number",
Expand All @@ -834,7 +834,7 @@ def test_fetch_fixed_version_space(self, calculator_demo):
"label": "output",
"type": {"type": "number"},
"python_type": {
"type": "int | float",
"type": "float",
"description": "",
},
"component": "Number",
Expand Down Expand Up @@ -950,7 +950,7 @@ def test_layout_and_state_components_in_output(
"label": "count",
"type": {"type": "number"},
"python_type": {
"type": "int | float",
"type": "float",
"description": "",
},
"component": "Number",
Expand All @@ -964,7 +964,7 @@ def test_layout_and_state_components_in_output(
"label": "count",
"type": {"type": "number"},
"python_type": {
"type": "int | float",
"type": "float",
"description": "",
},
"component": "Number",
Expand All @@ -978,7 +978,7 @@ def test_layout_and_state_components_in_output(
"label": "count",
"type": {"type": "number"},
"python_type": {
"type": "int | float",
"type": "float",
"description": "",
},
"component": "Number",
Expand Down
2 changes: 1 addition & 1 deletion client/python/test/test_utils.py
Expand Up @@ -160,7 +160,7 @@ def test_json_schema_to_python_type(schema):
elif schema == "BooleanSerializable":
answer = "bool"
elif schema == "NumberSerializable":
answer = "int | float"
answer = "float"
elif schema == "ImgSerializable":
answer = "str"
elif schema == "FileSerializable":
Expand Down
9 changes: 1 addition & 8 deletions test/requirements.txt
Expand Up @@ -232,13 +232,6 @@ traitlets==5.3.0
# matplotlib-inline
transformers==4.20.1
# via -r requirements.in
typing-extensions==4.7.1
# via
# black
# huggingface-hub
# pydantic
# starlette
# torch
urllib3==1.26.10
# via
# botocore
Expand All @@ -247,6 +240,6 @@ vega-datasets==0.9.0
# via -r requirements.in
wcwidth==0.2.5
# via prompt-toolkit

pydantic[email]
# The following packages are considered to be unsafe in a requirements file:
# setuptools

0 comments on commit 0c571c0

Please sign in to comment.